简体   繁体   中英

Exception while persisting a large object graph using JPA/Hibernate

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.

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:

  1. Create an order of conversion based on the dependencies of the domain models
  2. For each entity
    1. Execute database query to legacy DB
    2. Store new object for each obtained table row in a list in memory
  3. Iterate over generated lists in the same order as the conversion, and persist each entity.

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 Book and another would be Chapter defining a @ManyToOne(optional=false) relation to Book . Upon persisting the Chapter, it throws the exception java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: models.Chapter.book -> models.Book .

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 Book is set properly in the conversion of the Chapter , and I can also verify that all entities of type Book are persisted by the EntityManager before the entities of type Chapter get persisted. Obviously, my JPA provider does not behave as expected and does not truly persist my Book objects for some reason.

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 EntityManager s.

EDIT 1: Some additional info: I've again verified that entityManager.persist() is called on each of the book objects before 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.

EDIT 2: Because I don't think it's clear from the text above: the Book and Chapter story is just an example . It happens for any 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.

EDIT 3: The core issue seems to be that despite 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.

I once battled with such an error from hibernate myself. It turned out that it was a combination of a circle in the object graph and the cascade settings that caused the problem.

It has been a while so the fowlling might not be 100% accurate but maybe it is enough information to track your problem:

  1. Hibernate Wants to insert the chapter . Realizes it needs to insert the book first.
  2. Wants to insert the book . Realizes it needs to insert another entity first (eg publisher )
  3. Inserts publisher and performs cascades defined on publisher (eg authors )
  4. Author has eg reference to his lastestBook . Because hibernate internally already marked the book as processed (in step 2) you would no get an exception stating that author.book references a transient instance.

To find out if this is your problem you can enable full hibernate debugging and follow the path hibernate is taking through your object graph.

I've found the answer thanks to the discussion I've had with user1888440.

The solution to this answer was that the Spring @Transactional annotation was nonfunctional in my application. This mean that everything Hibernate did didn't occur in the context of a transaction. This meant that Hibernate would not set ids after persisting and this meant that all conversions would break down.

The reason why @Transactional did not work is probably because of a fact I did not mention: this script is part of a Play 2.0 (actually 2.1) app and is thus built using SBT. SBT doesn't use a normal Java setup to build an application, but instead uses the Scala compiler to compile Java as well. My guess is that the Scala compile did not work well with the AspectJ that Spring requires to make @Transactional work.

Instead, I performed all of the database work involved in this conversion within a programmatically defined Spring transaction (section 11.6). Now everything behaves as expected.

检查hbm文件中主键/对象ID的未保存值。如果您已通过hibernate框架自动创建ID,并且将ID放置在某处,则将抛出此错误。通过取消选中未保存的值是0,如果将ID设置为0,您会看到此错误。

Sounds like you are forgetting to assign a Book to each Chapter before persisting it. Even if you have persisted the Book it needs to be assigned to the #book property of the Chapter instance before you can persist the Chapter. This is because you have specified the relationship as non-optional. #book can never be null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM