admin管理员组

文章数量:1396739

I want to create a "preview" object and want to return the object to a rest entpoint. (String Json)

In Hibernate 6.3 I had no problems but in 6.6 I got the error message:

.hibernate.TransientObjectException: persistent instance references an unsaved transient instance of 'XXX' (save the transient instance before flushing)

I add cascade = {CascadeType.PERSIST} that helps in Hibernate 6.3 but not in 6.6.

The error message comes in a for-loop. The first run works, the error only occurs on the second run.

What can I do?

EDIT: I was able to narrow down the problem. I create object A, set a few properties and then go into the FOR loop and create object B. I attach object B to a list property of object A with ObjectA.getList().add(ObjectB). The loop should run through X times. The error occurs exactly on the second run.

The error occurs when I access a repository in the for loop and call a select (findByName()) to get more information for the ObjectB and set properties accordingly. Obviously Hibernate does not like that I call a repository / select without having saved the object before. Do I have to somehow annotate the repository method to be ReadOnly or something?

EDIT 2: I have now rebuilt my code so that I first read out all the necessary data and then create the object. Which then goes into performance, but I can't get it to run any other way. Unfortunately, my tests are now failing! Exactly the same thing happens. I create the object in the test without saving! And I want to test exactly that, with objectARepo.findAll().size() = 0. Again, I get the same error message. I have also tried objectARepo.detach(objectA) and objectARepo.flush(). Although the error then comes with objectARepo.flush().

By the way, why is there a DownVote here? What have I done wrong? I think it's unfair to downvote without saying what's wrong.

I want to create a "preview" object and want to return the object to a rest entpoint. (String Json)

In Hibernate 6.3 I had no problems but in 6.6 I got the error message:

.hibernate.TransientObjectException: persistent instance references an unsaved transient instance of 'XXX' (save the transient instance before flushing)

I add cascade = {CascadeType.PERSIST} that helps in Hibernate 6.3 but not in 6.6.

The error message comes in a for-loop. The first run works, the error only occurs on the second run.

What can I do?

EDIT: I was able to narrow down the problem. I create object A, set a few properties and then go into the FOR loop and create object B. I attach object B to a list property of object A with ObjectA.getList().add(ObjectB). The loop should run through X times. The error occurs exactly on the second run.

The error occurs when I access a repository in the for loop and call a select (findByName()) to get more information for the ObjectB and set properties accordingly. Obviously Hibernate does not like that I call a repository / select without having saved the object before. Do I have to somehow annotate the repository method to be ReadOnly or something?

EDIT 2: I have now rebuilt my code so that I first read out all the necessary data and then create the object. Which then goes into performance, but I can't get it to run any other way. Unfortunately, my tests are now failing! Exactly the same thing happens. I create the object in the test without saving! And I want to test exactly that, with objectARepo.findAll().size() = 0. Again, I get the same error message. I have also tried objectARepo.detach(objectA) and objectARepo.flush(). Although the error then comes with objectARepo.flush().

By the way, why is there a DownVote here? What have I done wrong? I think it's unfair to downvote without saying what's wrong.

Share Improve this question edited Mar 27 at 12:59 Burner asked Mar 26 at 12:39 BurnerBurner 1,0691 gold badge22 silver badges45 bronze badges 4
  • I assume you are referencing an entity from your persisted entity. As you correctly said, using CascadeType.PERSIST for the child entity reference is the way to go. Alternatively, you can also manually persist the referenced entity with .save() and then .save() the parent entity. This should avoid the error you're getting. If this doesn't help, please provide your entity definition(s) and the for-loop you use for persisting. – BullyWiiPlaza Commented Mar 26 at 13:37
  • It's exactly what I don't want. I don't want to save the object! Hibernate wants me to save it! – Burner Commented Mar 26 at 13:45
  • I updated my question, with more informations – Burner Commented Mar 27 at 8:48
  • 1 Can you please share some example code what happens before and inside your loop? The verbal description just is not precise enough IMHO. – cyberbrain Commented Mar 27 at 15:43
Add a comment  | 

1 Answer 1

Reset to default 0

I found the Solution for my UnitTest. There I can do

entityManager.clear();

and the PersistentCache will be cleared. Does not solve the actual problem of why you are not allowed to call a repository method.

本文标签: javaHow to create a complex entity without savein Hibernate 66Stack Overflow