gpt4 book ai didi

java - 为什么 onFlushDirty 将地址检测为脏但在 EmptyInterceptor 中未检测到地址

转载 作者:行者123 更新时间:2023-11-30 08:30:15 25 4
gpt4 key购买 nike

Contact Interceptor 附加 hibernate 配置

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;

}

联系人.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;


}

主.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 是否不检测脏对象?还是我遗漏了什么?

我主要关心的是当 child 变脏或修改父修改日期时也应该更改

最佳答案

脏对象是在刷新时保存/更新的对象,而不是与它们关联的对象。否则所有与 Contact 相关联的对象也应该被认为是脏的,因为它们包含的联系人已经“改变”,然后与这些对象相关联的所有对象等有效地意味着数据库的很大一部分可以得到被认为是脏的,因为您添加了一个新地址。

唯一的解决方案是手动编写关联对象更改时执行的逻辑,因为 Hibernate 无法了解应用程序的特定业务规则。

关于java - 为什么 onFlushDirty 将地址检测为脏但在 EmptyInterceptor 中未检测到地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41375441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com