Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Using DescriptorEventAdapter in @EntityListeners correct
Using DescriptorEventAdapter in @EntityListeners correct [message #1239571] Tue, 04 February 2014 13:56
Jack Kilian is currently offline Jack KilianFriend
Messages: 40
Registered: March 2012
Member
During I call commit() on my Transaction I want to check all the changed objects and add additional objects to the EntityManager to be stored, like the following example.


@Entity
@EntityListeners(MyDescriptorEventAdapter.class)
public class Order {

   @Id
   @Column(name="ID")
   private Integer id;

   @OneToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "RL _ID", referencedColumnName = "ID")
   private RecordLogger recordLogger = null;
	
   @Column(name="DESCRIPTION")
   private String description = null;

   [...]

   private RecordLogger getRecordLogger() {
      if(this.recordLogger == null) {
        this.recordLogger = new RecordLogger();
      }
      return this.recordLogger;
   }

   private void setRecordLogger(RecordLogger recordLogger) {
      this.recordLogger = recordLogger;
   }

}

@Entity
@EntityListeners(MyDescriptorEventAdapter.class)
public class RecordLogger {

    @Id
    @Column(name="ID")
    private Integer id;
	
    @Column(name="CHANGE_ME")
    private Timestamp changeMe = null;

    [...]
	
    private Timestamp getChangeMe() {
      return this.changeMe;
    }
	
    private void setChangeMe(Timestamp changeMe) {
      this.changeMe = changeMe;
    }	
}

public class MyDescriptorEventAdapter extends DescriptorEventAdapter {

    @Override
    public void aboutToUpdate(DescriptorEvent event) {
        System.out.println("### aboutToUpdate: " + event.getObject().getClass().getName());
        ((Order)event.getObject()).getRecordLogger().setChangeMe(new Timestamp(System.currentTimeMillis()));
        super.aboutToUpdate(event);
    }

    @Override
    public void preUpdate(DescriptorEvent event) {
        System.out.println("### preUpdate: " + event.getObject().getClass().getName());
        ((Order)event.getObject()).getRecordLogger().setChangeMe(new Timestamp(System.currentTimeMillis()));
        super.preUpdate(event);
    }

    @Override
    public void preUpdateWithChanges(DescriptorEvent event) {
        System.out.println("### preUpdateWithChanges: " + event.getObject().getClass().getName());
        ((Order)event.getObject()).getRecordLogger().setChangeMe(new Timestamp(System.currentTimeMillis()));
        super.preUpdateWithChanges(event);
    }

}


(This is an example how I implemented my classes)

I implemented a DescriptorEventAdapter as used in @EntityListeners.

In my application I changed the Object with the name Order. Afterwards I call EntityManager.commit().
My goal is now to update the Object Order.getRecordLogger().setChangeMe(new Timestamp(System.currentTimeMillis()));
I override the methods preUpdateWithChanges(), preUpdate() and aboutToUpdate() in my DescriptorEventAdapter to do this. All the Methods are called for the Order object.

My problem is now, that within the current transaction the Object getRecordLogger is not recognized as changed and not updated by the EntityManager.commit() process. Is this a problem of casading?

Is there a chance to change the Sub-Object RecordLogger ((Order)event.getObject()).getRecordLogger() in a manner, that it is commited within the current transaction when I change the RecordLogger object within a running transaction?

[Updated on: Tue, 04 February 2014 15:42]

Report message to a moderator

Previous Topic:JAXBErrorHandler#warning
Next Topic:Calculate Unit of Work before open Transaction
Goto Forum:
  


Current Time: Wed Apr 24 18:27:13 GMT 2024

Powered by FUDForum. Page generated in 0.02834 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top