question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use the PostgreSQLEnumType with both PostgreSQL and H2?

See original GitHub issue

Is there any way to allow PostgreSQLEnumType to work with a h2 database as well?

I use h2 in testing, where the regular @Enumerated(EnumType.STRING) works fine, however, it doesn’t work for the postgres database I use in production.

Your PostgreSQLEnumType works great for postgres, but doesn’t work for h2.

Is there anything you suggest for this use case?

Many thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Cytrioxcommented, Oct 15, 2020

Better with :

public class MyPostgreSQLEnumType extends PostgreSQLEnumType {

    private static final String H2_DIALECT = "org.hibernate.dialect.H2Dialect";

    @Override
    public void nullSafeSet(final PreparedStatement st, final Object value, final int index,
            final SharedSessionContractImplementor session) throws SQLException {

        // H2 database compatibility
        if (H2_DIALECT.equals(session.getFactory().getJdbcServices().getDialect().toString())) {
            st.setObject(index, value != null ? ((Enum<?>) value).name() : null, Types.VARCHAR);
        } else {
            super.nullSafeSet(st, value, index, session);
        }
    }

}

Have fun

0reactions
vladmihalceacommented, Jan 22, 2022

Schema validation happens in Hibernate ORM. This library provides extra Types. It does not change Hibernate ORM source code.

Anyway, if you use Hibernate column definition or hbm2ddl, you are doing it all wrong as you should have used something like Flyway instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

H2Database:MODE=PostgreSQL: doesn't support ENUM type
I want to use H2 with PostgreSQL dialect. My H2 version: "com.h2database" % "h2" ... create type myEnum as enum ('one', 'two', 'three');....
Read more >
Enum Mappings with Hibernate - The Complete Guide
Let me show you how to use Hibernate's standard enum mappings, create a flexible, custom mapping and map db-specific enum types.
Read more >
getting H2/Hibernate to recognize the Postgresql "text" type as ...
I'm having trouble getting H2/Hibernate to recognize the Postgresql "text" type. I have defined a column in Java as an enumerated type,
Read more >
The best way to map an Enum Type with JPA and Hibernate
To map the Enum to a String database column type, you need to specify the EnumType.STRING value when using the @Enumerated annotation. 1....
Read more >
Documentation: 15: 8.7. Enumerated Types - PostgreSQL
Enum types are created using the CREATE TYPE command, for example: ... 8.7.2. Ordering. The ordering of the values in an enum type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found