0

Using EJB3 JPA with Spring/Hibernate and a Postgres database.

I have a loop in which I am creating multiple instances of an Entity and persisting these using the EntityManager, within one (large) transaction.

I want to be able to do a JPQL select query during this process to find other data to add to these Entities. However, when I call the getResultList of the Query instance, I get the dreaded "unsaved transient" exception related to the Entity that I'm in the middle of creating.

Is there any way to make the query/Hibernate ignore the unsaved transients and just do the select query? The query is not related to the Entity that I'm in the middle of creating, so I don't know why it is complaining.

The method which performs the Query is marked @Transactional(propagation=SUPPORTS, readOnly=true) so the query should be executing within the same transaction/session as the creation of the Entities.

DuncanKinnear
  • 4,563
  • 2
  • 34
  • 65
  • Not really. The actual code is a lot more convoluted than my simplified description above. I'm trying to create a simple example of the same thing so that I can reproduce it. Might take a while, but I'll edit my question once I recreate it. – DuncanKinnear Apr 10 '12 at 01:11
  • where is the error stack trace ? Can you post that ? – Hardik Mishra Apr 10 '12 at 06:36
  • Sorry, logs have all been purged and I've got round the problem by changing the method which performs the query to `@Transactional(propagation=NOT_SUPPORTED, readOnly=true)`. Would still like to know why Hibernate thinks it needs to auto commit the transaction at the point where the query is executed. – DuncanKinnear Apr 10 '12 at 23:13

0 Answers0