Spring Boot & Spring Data: how are Hibernate Sessions managed?

I think I’ve found the answer myself. If somebody finds this question, here’s my answer. How does Spring manage Hibernate Sessions? By default, Spring Boot applies transaction management at the repository level. In this case, when calling a JpaRepository method (or in general any Repository method), Spring will: Ask the SessionFactory to create a new … Read more

Hibernate throws org.hibernate.AnnotationException: No identifier specified for entity: com..domain.idea.MAE_MFEView

You are missing a field annotated with @Id. Each @Entity needs an @Id – this is the primary key in the database. If you don’t want your entity to be persisted in a separate table, but rather be a part of other entities, you can use @Embeddable instead of @Entity. If you want simply a … Read more

JPQL Constructor Expression – org.hibernate.hql.ast.QuerySyntaxException:Table is not mapped

according to the book “Pro EJB 3 Java Persistence API” Constructor Expressions A more powerful form of SELECT clause involving multiple expressions is the constructor expression, which specifies that the results of the query are to be stored using a user-specified object type. Consider the following query: SELECT NEW example.EmployeeDetails(e.name, e.salary, e.department.name) FROM Employee e … Read more

How to use the Java 8 LocalDateTime with JPA and Hibernate

For any Hibernate 5.x users, there is <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-java8</artifactId> <version>5.0.0.Final</version> </dependency> You don’t need to do anything else. Just add the dependency, and the Java 8 time types should work like any other basic types, no annotations required. private LocalDateTime invalidateTokenDate; Note: this won’t save to timestamp type though. Testing with MySQL, it saves … Read more

How to connect to multiple databases in Hibernate

Using annotation mappings as an example: Configuration cfg1 = new AnnotationConfiguration(); cfg1.configure(“/hibernate-oracle.cfg.xml”); cfg1.addAnnotatedClass(SomeClass.class); // mapped classes cfg1.addAnnotatedClass(SomeOtherClass.class); SessionFactory sf1 = cfg1.buildSessionFactory(); Configuration cfg2 = new AnnotationConfiguration(); cfg2.configure(“/hibernate-mysql.cfg.xml”); cfg2.addAnnotatedClass(SomeClass.class); // could be the same or different than above cfg2.addAnnotatedClass(SomeOtherClass.class); SessionFactory sf2 = cfg2.buildSessionFactory(); Then use sf1 and sf2 to get the sessions for each database. For … Read more