Hibernate/JPA ManyToOne vs OneToMany

Suppose you have an Order and an OrderLine. You can choose to have a unidirectional OneToMany between Order and OrderLine (Order would have a collection of OrderLines). Or you can choose to have a ManyToOne association between OrderLine and Order (OrderLine would have a reference to its Order). Or you can choose to have both, … 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

org.hibernate.QueryException: illegal attempt to dereference collection

billProductSet is a Collection. As such, it does not have an attribute named product. Product is an attribute of the elements of this Collection. You can fix the issue by joining the collection instead of dereferencing it: SELECT count(*) FROM BillDetails bd JOIN bd.billProductSet bps WHERE bd.client.id = 1 AND bps.product.id = 1002

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

Proper way of writing a HQL in ( … ) query

I am unsure how to do this with positional parameter, but if you can use named parameters instead of positional, then named parameter can be placed inside brackets and setParameterList method from Query interface can be used to bind the list of values to this parameter. … Query query = session.createQuery(“FROM Cat c WHERE c.id … 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

How do you create a Distinct query in HQL

Here’s a snippet of hql that we use. (Names have been changed to protect identities) String queryString = “select distinct f from Foo f inner join foo.bars as b” + ” where f.creationDate >= ? and f.creationDate < ? and b.bar = ?”; return getHibernateTemplate().find(queryString, new Object[] {startDate, endDate, bar});

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)