JPA temporary information was lost during creation

I have an entity with a transient field When I want to create a new object instance, I lose my transient information The following example illustrates this problem For this example, let's say it is a short field

FooEntity fooEntity = new FooEntity();
fooEntity.setFoobosity(5);
fooEntity.setBarness(2);
fooEntity = fooEntityManager.merge(fooEntity);
System.out.println(fooEntity.getFoobosity()); //5
System.out.println(fooEntity.getBarness()); //0 (or whatever default barness is)

Is there any way to keep my short message?

Solution

This works more or less according to the design The semantics of transient is 07000 From entitymanager The entity returned by merge (obj) is actually a new entity, which keeps the state of the object passed to merge (state, in this case, is not part of anything persistent object) This was explained in detail in January 2007 Note: after object merging, there may be JPA implementation to maintain transient fields (just because they return the same object), but this behavior cannot be guaranteed by the specification

Basically, you can do two things:

>Determine the continuous transient field If you need a class after merging it into the persistence context, it doesn't seem to be transient. > Maintains the value of the transient field outside the permanent object If this meets your needs, you may need to rethink the structure of domain classes; If this field is not part of the domain object state, it really shouldn't be there

One last thing: I found that the main use case of transient fields on domain classes is to divide derived fields, that is, fields that can be recalculated according to the persistent fields of the class

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>