I have an entity which contains several basic fields A, B and C and one complex field X which is not (should not be) mapped to a database column. The idea is to create X from A, B and C when loading an instance, and to deconstruct X into A, B and C when persisting.
The first logical thing to try was something like:
@Entity class Xxx {
private String a;
@Transient private X x;
@PrePersist public void prePersist() { XParts xp = x.deconstruct(); a = xp.getA(); //... } //... }
The problem is, when calling entityManager.merge(xxx); x is lost. Something to do with how the @Transients are handled in JPA. @Transient and @PrePersist don't mix. Ok. Can't argue with the specification I guess.
But, what are the alternatives? I really don't need X in database.
The best I could come up with is:
@Column(insertable=false,updatable=false) private X x;
Which creates column X, which is always null. If it's really the only way, I can live with that, but it seems hackish at best.
I can't be the only one who run into this problem. Is there any better way to do this?
Or maybe - that would be preferable - there is a way to make @Transient and @PrePersist work together?
|