聯繫攔截聯繫不上附有Hibernate配置爲什麼onFlushDirty檢測地址爲髒,但在EmptyInterceptor
public class ContactInterceptor extends EmptyInterceptor {
/**
*/
private static final long serialVersionUID = -2000639365689865828L;
@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
if (entity instanceof LastModifiable) {
if (!Arrays.equals(currentState, previousState)) {
int index = ArrayUtils.indexOf(propertyNames, "modified");
if (index > 0) {
currentState[index] = new Date();
return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types);
}
}
}
return false;
}
}
地址模型
public class Address extends Model {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@OneToOne
@JoinColumn(referencedColumnName = "id", name = "contact_id")
private Contact contact;
@Column(name = "address")
private String address1;
@Column(name = "address2")
private String address2;
@Column(name = "city")
private String city;
@Column(name = "country")
private String country;
@Column(name = "state")
private String state;
@Column(name = "zipcode")
private String zipcode;
@Column(name = "company")
private String company;
@Override
public Long getId() {
return this.id;
}
@Override
public void setId(Long val) {
this.id=val;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created", columnDefinition = "DATETIME")
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.modified = this.created = created;
}
private Date created = null;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "modified", columnDefinition = "DATETIME")
public Date getModified() {
return modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
private Date modified;
}
Contact.java
public class Contact extends Model {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Transient
private String fullName;
@Column(name = "email")
private String email;
@Column(name = "contact")
private String contact;
@JsonIgnore
@OneToMany(mappedBy = "contact", fetch = FetchType.EAGER)
protected Set<Address> addresses = new HashSet<Address>(0);
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public Set<Address> getAddresses() {
return addresses;
}
public void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created", columnDefinition = "DATETIME")
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.modified = this.created = created;
}
private Date created = null;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "modified", columnDefinition = "DATETIME")
public Date getModified() {
return modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
private Date modified;
}
Main.java
public static void main(String[] args) {
HibernateUtility u = new HibernateUtility();
u.beginTransaction();
Contact c = DBService.getDBService(Contact.class).load(112L);
Address add = new Address();
add.setAddress1("Dummy Address 1");
add.setCity("AHMD");
add.setState("GUJ");
add.setCreated(new Date());
add.setCountry("INR");
add.setContact(c);
c.getAddresses().add(add);
// c.setModified(new Date());
u.endTransaction();
}
我們知道,onFlushDirty是當檢測對象是骯髒jboss Doc
但是,當我在添加聯繫人地址,該地址具有類型的集合調用,調試,我可以看到地址是髒的過程中(如新記錄將插入),但爲什麼聯繫在這裏不髒?
在收集修改的情況下,hibernate是否檢測不到髒對象?或者我錯過了什麼?
我的主要關注點是當孩子髒或修改父親修改日期也應改變。
@UpdateTimestamp只有在我更改Contact.java實體中的任何內容時才起作用,但如果我添加新地址,則不會修改聯繫人 –
同意,您應該手動執行此操作,例如修改地址實體修改日期的add方法。 – idmitriev
絕對手動是最糟糕的情況。我想要的是使用單個控制器來管理此類事件。有沒有辦法檢查DefaultSaveOrUpdaeEvent中的對象是否髒? –