Manticore
Manticore

Reputation: 480

JPA never call update, but persist work

I'm encountering a problem with JPA. I have three entities; the first (Event) works fine. I present only the header:

@Entity
@EntityListeners({EventSupervisionListener.class})
public class Event extends Exportable {
// Some code

The second extends the first and implements a interface. This one doesn't work very well. I can persist a new entity, but the update method doesn't work.

@Entity
public class RemoteEvent extends Event implements RemoteEntity {

    @ManyToOne(optional=false)
    private Remote net;

    @Override
    public Remote getRemote() {
        return cellnet;
    }

    @Override
    public void setRemoteCellNet(Remote net) {
        this.net= net;
    }
}

Finally the remote entity, which also works fine:

@Entity
@EntityListeners({RemoteListener.class})
public class Remote extends Device  {
    @ManyToOne(fetch= FetchType.EAGER, optional=true, cascade= CascadeType.ALL)
    private RemoteArea remoteRootArea;
    // A lots of contain but no reference with RemoteEvent or Event

For the update I have a simple method:

// Get a event object :
RemoteEvent event = getEvent();
em.find(RemoteEvent.class, event.getId());
// Event is find (test with em.contain).
event.setName("test");

That's not working; I cannot understand why...

Thanks for helping

[EDIT new contents after some research]

I have simplify the problem and focus on the method who create the error. I have this easy hierarchy :

@MappedSuperclass
public abstract class Exportable implements Serializable, UUIDOwner {

@Entity
@EntityListeners({EventSupervisionListener.class})
public class Event extends Exportable {

@Entity
public class RemoteEvent extends Event{
// This entity do nothing except extend Event.

The problem seem to come from this part of Event, in debug mode i can reach @postLoad method, but the onUpdate is never call. I can bypass the problem with comment the json part of loadImageGroup method.

@Lob
private String images;

@ExtJsSerialize
private transient List<EventImageGroup> imageGroupList = new ArrayList<EventImageGroup>();

@PreUpdate
@PrePersist
private void saveImageGroups() {
    JSONArray json = new JSONArray();
    for (EventImageGroup imgGroup : imageGroupList) {
        json.put(imgGroup.toJson());
    }
    images = json.toString();
}

@PostLoad
private void loadImageGroups() {
    try 
    {
        imageGroupList.clear();
        // If i comment this instruction the code working fine. 
        // There are no exception rise.
        JSONArray json = new JSONArray(images);
        for (int i = 0; i < json.length(); ++i) { 
            imageGroupList.add(new EventImageGroup(json.getJSONObject(i)));
        }
    } 
    catch (JSONException ex) {
        Logger.getLogger(Event1.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I precise i have exactly the same value in database for a event object and a remoteEvent except the dtype.

I can't understand why a extends without any change can lead to problem...

Thanks

Upvotes: 0

Views: 243

Answers (1)

V G
V G

Reputation: 19002

That is because simply calling find() does not mark your instance event managed: any changes you make on that instance are not tracked/recognized by your JPA provider. In order to fix that, you should do the changes on the returned entity:

// Get a event object :
RemoteEvent event = getEvent();
RemoveEvent eventDb = em.find(RemoveEvent.class, event.getId());
eventDb.setName("test");//note here the returned instance is used
//DO NOT FORGET TO COMMIT if necessary

Upvotes: 1

Related Questions