title: Exception while persisting a large object graph using JPA/Hibernate
body: <p>We are creating a new web application backed by JPA to replace an old web application. As part of the migration we are converting the old application's database to a new, more sophisticated, JPA-managed database.</p>
  
  <p>So I've written a 'script' that converts the old database to a set of JPA entities and subsequently saves them. It works like this:</p>
  
  <ol>
  <li>Create an order of conversion based on the dependencies of the domain models</li>
  <li>For each entity
  <ol>
  <li>Execute database query to legacy DB</li>
  <li>Store new object for each obtained table row in a list in memory</li>
  </ol></li>
  <li>Iterate over generated lists in the same order as the conversion, and persist each entity.</li>
  </ol>
  
  <p>Now, the first two steps work well. Upon persisting, however I get an exception. The exception occurs when one entity has a relation to another entity. For example if one of our entities would be a <code>Book</code> and another would be <code>Chapter</code> defining a <code>@ManyToOne(optional=false)</code> relation to <code>Book</code>. Upon persisting the Chapter, it throws the exception <code>java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: models.Chapter.book -&gt; models.Book</code>.</p>
  
  <p>Of course, this indicates that something is wrong with the state of the book: it seems it is either not set or has not yet been persisted. However, I can verify that the <code>Book</code> is set properly in the conversion of the <code>Chapter</code>, and I can also verify that all entities of type <code>Book</code> are persisted by the <code>EntityManager</code> <em>before</em> the entities of type <code>Chapter</code> get persisted. Obviously, my JPA provider does not behave as expected and does not truly persist my <code>Book</code> objects for some reason.</p>
  
  <p>What solution would allow me to save the entire graph of objects that I have converted to the database? I use Hibernate as my JPA provider and I also use Spring 3.1 for injection of dependencies and <code>EntityManager</code>s.</p>
  
  <p>EDIT 1: Some additional info: I've again verified that entityManager.persist() is called on each of the book objects <em>before</em> entityManager.persist() is called on the chapters. However, the id of the book object remains null, meaning it is not properly persisted. The database also remains empty, despite not using transactions.</p>
  
  <p>EDIT 2: Because I don't think it's clear from the text above: the Book and Chapter story is <em>just an example</em>. It happens for <em>any</em> entity that references another entity. This makes it seem as if I'm not using JPA/Hibernate properly as opposed to not setting the values of my entities properly.</p>
  
  <p>EDIT 3: The core issue seems to be that <em>despite</em> persisting Book properly, having all the right annotations, book.getId() remains null. Basically, Hibernate is not setting the ids on my entities after persisting them, leading to problems when I need to use those entities later.</p>