-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use PostgreSQLEnumType
with Hibernate 6?
#514
Comments
The Here's the Hibernate 6
You don't really have to do anything special. It basically works as before, even if the Hibernate
That was never required by Hibernate Types. The PostgreSQL enum type is the responsibility of the DB schema tool, like Flyway. Hibernate Types doesn't need it. |
Hi @vladmihalcea, thank you for this example provided and all your effort. I have a question, do I get it right that |
@BParnikel The As for |
@vladmihalcea thank you for quick response. I agree this behaviour makes sense and with Flyway we'd just use Re exception, I think it is related to ddl as wrapping exception is |
If you are using a VARCHAR column to store the Enum, then just use the default Hibernate type. No need to use the |
@vladmihalcea I have Postgres Enum column in prod database, so Upd: looks like |
Not using the same DB for testing is very bad. Thanks to Testcontainers, it's very easy to use the same DB engine for tests. |
PostgreSQLEnumType is broken with spring boot 3.1.1 |
@avinashjeevanandham No, it's not broken at all. Here's the proof that it works just fine with Hibernate 6.2. |
Sorry, i was not aware of this new module. I was using |
@vladmihalcea I am running into an issue after migration of existing project to Spring Boot 3 and Java 17, I am using the following dependency . I would appreciate your help
implementation (group: 'com.vladmihalcea', name: 'hibernate-types-60', version: '2.20.0')
The error Message I am getting on running the application is as - APPLICATION FAILED TO START Description: An attempt was made to call a method that does not exist. The attempt was made from the following location:
The following method did not exist:
|
@rs10615 This test case shows you that the |
Hello, I am getting the same issue :
I am using Spring boot v3.2.0 that use hibernate v6.3.1 @rs10615 Did you managed to fix this ? Thanks in advance for the help 😄 |
@brandonfl I was stuck with a similar issue before I realized that PostgreSQLEnumType has been So this should work (kotlin code):
As opposed to former PostgreSQLEnumType, the postgres type name was not translated from CamelCase to under_score case automatically, so I had to rename the enum class, but there's hopefully a better way to control this. Related issue: Add a hypersistence-utils-hibernate-63 module for Hibernate 6.3 #657 |
Thanks for the help |
Hi there, I am using spring boot v3.2.0 and hibernate v6.3.1 And tring to use JdbcType annotation for my enum field: enum STATUS {
DRAFT, NEW, IN_PROGRESS, COMPLETED, REJECTED;
}
@Enumerated(EnumType.STRING)
@Column(name = "status")
@JdbcType(PostgreSQLEnumJdbcType.class)
private STATUS status; Next, I wrote a method in my Repository interface with native query as follows: @Query(nativeQuery = true, value = "SELECT * FROM requests r WHERE r.status IN (?1)")
List<Request> getRequestsByStatus(List<Request.STATUS> statuses); When I call the
Oh, yeah. In the database, the Digging a bit into the Hibernate code, I found the following: private static <E extends Enum<E>> BasicTypeImpl<E> createEnumType(ExecutionContext executionContext, Class<E> enumClass) {
final EnumJavaType<E> enumJavaType = new EnumJavaType<>( enumClass );
final JdbcTypeIndicators indicators =
executionContext.getSession().getTypeConfiguration().getCurrentBaseSqlTypeIndicators();
final JdbcType jdbcType =
// we don't know whether to map the enum as ORDINAL or STRING,
// so just accept the default from the TypeConfiguration, which
// is usually ORDINAL (the default according to JPA)
enumJavaType.getRecommendedJdbcType(indicators);
return new BasicTypeImpl<>( enumJavaType, jdbcType );
} As I understand it, the problem is to specify JdbcType as STRING instead of ORDINAL, but I don't know how to do that. |
@kaperusov Do you need a cast in the Native Query? (cast as pg_enum_whatever_name)? |
@kaperusov It seems that you need to use the annotation
|
In Hibernate 6,
org.hibernate.annotations.TypeDef
has been removed, andorg.hibernate.annotations.Type
has been changed.Therefore, this tutorial Is unfortunately not relevant anymore.
How to set the enum type with Hibernate 6 and
PostgreSQLEnumType
?We can set the class in the
@Type
annotation on an enum field, like this, but how do we specify the name of the enum type in the PostgreSQL Database? Earlier it was in
@Type(type)
and@TypeDef(name)
annotation attributes.The text was updated successfully, but these errors were encountered: