How to use Spring managed Hibernate interceptors in Spring Boot?

There’s not a particularly easy way to add a Hibernate interceptor that is also a Spring Bean but you can easily add an interceptor if it’s managed entirely by Hibernate. To do that add the following to your application.properties: spring.jpa.properties.hibernate.ejb.interceptor=my.package.MyInterceptorClassName If you need the Interceptor to also be a bean you can create your own … Read more

Hibernate Criteria Join with 3 Tables

The fetch mode only says that the association must be fetched. If you want to add restrictions on an associated entity, you must create an alias, or a subcriteria. I generally prefer using aliases, but YMMV: Criteria c = session.createCriteria(Dokument.class, “dokument”); c.createAlias(“dokument.role”, “role”); // inner join by default c.createAlias(“role.contact”, “contact”); c.add(Restrictions.eq(“contact.lastName”, “Test”)); return c.list(); This … Read more

hibernate: LazyInitializationException: could not initialize proxy

The problem is that you are trying to access a collection in an object that is detached. You need to re-attach the object before accessing the collection to the current session. You can do that through session.update(object); Using lazy=false is not a good solution because you are throwing away the Lazy Initialization feature of hibernate. … Read more

How does JPA orphanRemoval=true differ from the ON DELETE CASCADE DML clause

orphanRemoval has nothing to do with ON DELETE CASCADE. orphanRemoval is an entirely ORM-specific thing. It marks “child” entity to be removed when it’s no longer referenced from the “parent” entity, e.g. when you remove the child entity from the corresponding collection of the parent entity. ON DELETE CASCADE is a database-specific thing, it deletes … Read more

How to generate Custom Id using hibernate while it must be primary key of table

Thank you everyone for your response…… finally i have done some changes in my Department class and used a class for generating ids……..Here is my code @Entity public class Department { @Id @GenericGenerator(name = “sequence_dep_id”, strategy = “com.xyz.ids.DepartmentIdGenerator”) @GeneratedValue(generator = “sequence_dep_id”) @Column(name=”Department_Id”) private String deptId; @Column(name=”Department_Name”,unique=true,nullable=false) private String deptName; @Column(name=”Department_Description”) @NotNull private String deptDesc; //getters … Read more

Hibernate: Difference between session.get and session.load

From the Hibernate forum: This from the book Hibernate in Action. Good one read this.. Retrieving objects by identifier The following Hibernate code snippet retrieves a User object from the database: User user = (User) session.get(User.class, userID); The get() method is special because the identifier uniquely identifies a single instance of a class. Hence it’s … Read more

How to do bulk (multi row) inserts with JpaRepository?

To get a bulk insert with Spring Boot and Spring Data JPA you need only two things: set the option spring.jpa.properties.hibernate.jdbc.batch_size to appropriate value you need (for example: 20). use saveAll() method of your repo with the list of entities prepared for inserting. Working example is here. Regarding the transformation of the insert statement into … Read more