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 |1 Answer
Reset to default 0I 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
版权声明:本文标题:java - How to create a complex entity without save, in Hibernate 6.6? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744144548a2592772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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