I have BaseEntity and i want to set some property in all hibernate add and update ,for do this request we can do that with this code
@MappedSuperclass
@EntityListeners(BaseEntityListener.class)
public abstract class BaseEntity<T> implements Serializable {
private static final long serialVersionUID = 4295229462159851306L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private T id;
@Column(name = "version")
private Integer version;
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "createdby", updatable = false)
private User createdBy;
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "updatedby")
private User updatedBy;
@Column(name = "createddate", updatable = false)
private Date createdDate = new Date();
@Column(name = "updateddate")
private Date updatedDate = new Date();
@Column(name = "ip")
private String ip = "127.0.0.1";
,..
}
public class BaseEntityListener {
@PrePersist
@PreUpdate
private void initializeCreatedAt(BaseEntity baseEntity) {
System.out.println("BaseEntityListener");
baseEntity.setIp(SecurityUtility.getRequestedIp());
baseEntity.setCreatedBy(SecurityUtility.getAuthenticatedUser());
baseEntity.setUpdatedBy(SecurityUtility.getAuthenticatedUser());
baseEntity.setCreatedDate(new Date());
baseEntity.setUpdatedDate(new Date());
}
}
in hibernate 5 EntityListeners dose not work