LifeCycle Event with Embeddable classes

#1

Hello,

is there a way that lifecycle events like prepersist or preupdate are also fired for embeddable classes, not just for entities?

My case looks as follows:

@Entity
public class Foo  {

@Id
@GeneratedValue
private Long id;

private String description;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Bar> barList;

@Temporal(TemporalType.TIMESTAMP)
private Timestamp modifyDate;

@PrePersist
@PreUpdate
void onPreUpdate() {
  modifyDate = new Timestamp(System.currentTimeMillis());
}}

@Embeddable
public class Bar {

@Temporal(TemporalType.TIMESTAMP)
private Timestamp modifyDate;

private Integer priority;

private String comment;

@PrePersist
@PreUpdate
void onPreUpdate() {
  modifyDate = new Timestamp(System.currentTimeMillis());
}}

When i update oder create Entity Foo AND Bar, onPreUpdate() is triggered on Foo, but not on Bar (even if priority or comment have changed)

Is there a solution to this problem? What does the JPA standard say?

Greetings

ObjectArts

 

 

#2

So far any idea?

My previous solution is to make the Bar class an Entity class though it does not make sense practically.

I cannot setModifyDate manually and i think preupdate and prepersist annotation are a quite elegant way of setting these properties.

ObjectArts

 

#3

JPA lifecycle events are not applicable for embeddable classes. You can set an event callback for the containing entity and use it to handle embedded objects in the entity object.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login