0

I have some problems with storing this simple mapping:

@Entity
public class Account extends UUIDBase {
    private Profile profile;

    @OneToOne(cascade = CascadeType.ALL, optional = false)
    public Profile getProfile() {
       return profile;
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }
}

@Entity
public class Profile extends UUIDBase {
  ...
}

Our entities have all the attribute "creationDate" and "lastUpdated". These attributes are placed in the mapped superclass UUIDBase. When a entity is persisted or updated both fields will be updated in @PrePersist and @PreUpdate callback. This works fine except in the case of cascading.

When we store the Account the Profile will always be stored, too. The creationDate and lastUpdated attribute of the account will be initialized through the callback methods. The callbacks methods for the Profile will not be called. Do you have a hint what´s going wrong?

  • I have no clue why this is not working for you. Could you specify which jpa implementation you are using? It might help to replace the tag *callback* with the framework name. – Augusto Jun 15 '11 at 08:42
  • Hi, we use JPA with Eclipselink V2.2 implementation. –  Jun 15 '11 at 09:27
  • I found a similar question but I´m not sure if this belongs to my problem and how I could solve the problem. http://stackoverflow.com/questions/1877499/eclipselink-jpa-preupdate-call-not-persisting –  Jun 15 '11 at 10:12

2 Answers2

0

They should be called. Ensure that you are registering the callbacks correctly.

There was an issue with callbacks on a MappedSuperclass not getting called that was fixed recently, so you may need to add the callbacks to the subclass in 2.2.

Are you sure the callbacks are not called, or do the values just not get updated?

If you directly call persist on the profile is the callback called?

How do you register the callback?

James
  • 17,965
  • 11
  • 91
  • 146
0

Thank you for your answer. I think I fixed it now. I described the two entities "Account" and "Profile" above. The entity "Employee" is missing in my description above. Here is the mapping:

@Entity
public class Employee extends UUIDBase {
    public Account account;

    @OneToOne(cascade = {CascadeType.REFRESH, CascadeType.REMOVE}, orphanRemoval = true)
    public Account getAccount() {
        return this.account;
    }

    public void setAccount(Account account) { 
        this.account = account;
    } 

}

The mapping is "A employee could have a account. A account must have a profile". The problem is in the service class:

public void saveEmployee(Employee data) {
    Employee savedEmployee = empDao.saveEmployee(data);
    accountService.saveAccount(data.getAccount()); <-- Here is the failure
}

First I save the employee and get the saved employee object back. After that I try to save the employees account through its own service. When I take the account of the saved employee object everything works and the callbacks are called. When I take the account from the "data" parameter the callbacks are not called.