EclipseLink JPA Changes

I am trying to log any changes to my JPA entities. For this reason, each object inherits from an abstract entity class that has a list of LogEntry objects.

AbstractEntity class:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@EntityListeners(ChangeListener.class)
public abstract class AbstractEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Version
    private Long version;
    @Temporal(TemporalType.DATE)
    private Date validFrom;
    @Temporal(TemporalType.DATE)
    private Date validTo;
    private String name;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "abstractEntity")
    private List<LogEntry> logEntry = new ArrayList<LogEntry>();
    //getter and setter
}

LogEntry Class:

@Entity
public class LogEntry extends AbstractEntity {

    @ManyToOne
    @JoinColumn
    protected AbstractEntity abstractEntity;
    @ManyToOne
    @JoinColumn
    protected Person person; // creator or updater
    @Column(updatable=false, insertable=false, columnDefinition="TIMESTAMP DEFAULT   CURRENT_TIMESTAMP")
    @Temporal(TemporalType.TIMESTAMP)
    protected Date changeDate;
    protected String comment;
    //getter and setter
}

My approach was to create a new LogEntry object and add it to the LogEntry list of the object before the entity was updated or saved.

I tried the following solutions:

  • Using callback annotations (@PreUpdate, @PrePersist, etc.) directly in an entity class or separated in an entity listener associated with AbstractEntity
  • EclipsLink DescriptorEvent . . preUpdate LogEntry . LogEntry , preUpdate ( preUpdate), . , unitOfWork . ( ) , ? , preUpdateWithChanges , , -, . .

(, validTo) . LogEntry , , LogEntry. bean jndi lookup LogEntry . jndi , bean .

:

public class ChangeListener extends DescriptorEventAdapter {

    @Override
    public void preUpdate(DescriptorEvent event) {
        AbstractEntity entity = (AbstractEntity) event.getObject();
        if (!(entity instanceof LogEntry)) {
            LogEntry logEntry = new LogEntry();
            logEntry.setPerson(getSessionController().getCurrentUser());
            logEntry.setAbstractEntity(entity);
            entity.getLogEntries().add(logEntry);
        }
    }
}

Hibernate Envers .

EclipseLink 2.3.2.

+5
4
+7

, preUpdate. EqualsBuilder Apache Commons Lang.

public class ChangeAbstractEntityListener extends DescriptorEventAdapter {

    @Override
    public void preUpdate(DescriptorEvent event) {
        AbstractEntity originalEntity = (AbstractEntity) event.getOriginalObject();
        AbstractEntity entity = (AbstractEntity) event.getObject();

        if (!EqualsBuilder.reflectionEquals(originalEntity, entity, true)) {
            if (!(entity instanceof LogEntry)) {
                LogEntry logEntry = new LogEntry();
                logEntry.setPerson(getSessionController().getCurrentUser());
                logEntry.setAbstractEntity(entity);
                entity.getLogEntries().add(logEntry);
            }
        }
    }
    //jndi lookup to get the user object
}
+3

EclipseLink , . EclipseLink HistoryPolicy ClassDescriptor , . .

0

, , Hibernate Envers.

JPA ( Spring -Data) , , Spring .

Envers Hibernate 3.6

-1

All Articles