Hibernate mapping between PostgreSQL enum and Java enum

You can simply get these types via Maven Central using the Hypersistence Util dependency: <dependency> <groupId>io.hypersistence</groupId> <artifactId>hypersistence-utils-hibernate-55</artifactId> <version>${hibernate-types.version}</version> </dependency> If you easily map Java Enum to a PostgreSQL Enum column type using the following custom Type: public class PostgreSQLEnumType extends org.hibernate.type.EnumType { public void nullSafeSet( PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, … Read more

How do I map a BigDecimal in Hibernate so I get back the same scale I put in?

Just for informational sake, I can tell you that the creation of the BigDecimal coming back from the database is done by the proprietary JDBC driver’s implementation of the ‘getBigDecimal’ method of the database-specific ‘ResultSet’ sub-class. I found this out by stepping thru the Hibernate source code with a debugger, while trying to find the … Read more

UnsupportedOperationException: The application must supply JDBC connections

Wow, just fixed the problem. sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry()); I was missing the .applySettings(configuration.getProperties()) Learnings configure() should be called AFTER setProperty Use hibernate.connection.url and NOT connection.url if you use hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect Set log4j property for hibernate logs to ALL, so that you can see more detailed issues To get rid of the WARN Recognized obsolete hibernate … Read more

org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

Hibernate doesn’t allow fetching more than one bag because that would generate a Cartesian product. Now, you will find lots of answers, blog posts, videos, or other resources telling you to use a Set instead of a List for your collections. That’s terrible advice! Using Sets instead of Lists will make the MultipleBagFetchException go away, … Read more

Hibernate use of PostgreSQL sequence does not affect sequence table

I had the same problem. It is related to the id allocating strategies of Hibernate. Whe n you choose GenerationType.SEQUENCE, Hibernate uses HiLo strategy which allocates IDs in blocks of 50 by default. So you can explicitly set allocationSize value like this: @Id @SequenceGenerator(name=”pk_sequence”,sequenceName=”entity_id_seq”, allocationSize=1) @GeneratedValue(strategy=GenerationType.SEQUENCE,generator=”pk_sequence”) @Column(name=”id”, unique=true, nullable=false) public int getId() { return this.id; … Read more

How Do I Create Many to Many Hibernate Mapping for Additional Property from the Join Table?

You need to use @EmbeddedId and @Embeddable annotations to solve this issue: Lecturer Class: @Entity @Table(name=”LECTURER”) public class Lecturer { @OneToMany(fetch = FetchType.LAZY, mappedBy = “pk.lecturer”, cascade=CascadeType.ALL) Set<LecturerCourse> lecturerCourses == new HashSet<LecturerCourse>(); //all others properties Setters and getters are less relevant. } Course class: @Entity @Table(name=”COURSE”) public class Course { @OneToMany(fetch = FetchType.LAZY, mappedBy = … Read more