Skip to content

Commit

Permalink
Mapping Enum to a String column uses toString() instead of name() #135
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmihalcea committed Oct 8, 2019
1 parent 527c5ae commit b0112ed
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public void nullSafeSet(
int index,
SessionImplementor session)
throws HibernateException, SQLException {
st.setObject(index, value == null ? null : value.toString(), Types.OTHER);
st.setObject(index, value != null ? ((Enum) value).name() : null, Types.OTHER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ public Void apply(EntityManager entityManager) {
public enum PostStatus {
PENDING,
APPROVED,
SPAM
SPAM;

@Override
public String toString() {
return String.format("The %s enum is mapped to ordinal: %d", name(), ordinal());
}
}

@Entity(name = "Post")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public void nullSafeSet(
int index,
SessionImplementor session)
throws HibernateException, SQLException {
st.setObject(index, value, Types.OTHER);
st.setObject(index, value != null ? ((Enum) value).name() : null, Types.OTHER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ public Void apply(EntityManager entityManager) {
public enum PostStatus {
PENDING,
APPROVED,
SPAM
SPAM;

@Override
public String toString() {
return String.format("The %s enum is mapped to ordinal: %d", name(), ordinal());
}
}

@Entity(name = "Post")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public void nullSafeSet(
int index,
SessionImplementor session)
throws HibernateException, SQLException {
st.setObject(index, value, Types.OTHER);
st.setObject(index, value != null ? ((Enum) value).name() : null, Types.OTHER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ public Void apply(EntityManager entityManager) {
public enum PostStatus {
PENDING,
APPROVED,
SPAM
SPAM;

@Override
public String toString() {
return String.format("The %s enum is mapped to ordinal: %d", name(), ordinal());
}
}

@Entity(name = "Post")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ public void nullSafeSet(
int index,
SharedSessionContractImplementor session)
throws HibernateException, SQLException {
st.setObject(index, value, Types.OTHER);
st.setObject(index, value != null ? ((Enum) value).name() : null, Types.OTHER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ public void testTypedParameterValue() {
public enum PostStatus {
PENDING,
APPROVED,
SPAM
SPAM;

@Override
public String toString() {
return String.format("The %s enum is mapped to ordinal: %d", name(), ordinal());
}
}

@Entity(name = "Post")
Expand Down

0 comments on commit b0112ed

Please sign in to comment.