我有一个非常奇怪的问题,我们正在使用基于Hibernate注释的JPA 2.0,并且将MySQL作为数据库生成通过JPA DDL;
我会提供一些参考类,然后是我的问题。
@MappedSuperclass
public abstract class Common implements serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false)
private Long id;
@ManyToOne
@JoinColumn
private Address address;
//with all getter and setters
//as well equal and hashCode
}
@Entity
public class Parent extends Common{
private String name;
@OneToMany(cascade = {CascadeType.MERGE,CascadeType.PERSIST}, mappedBy = "parent")
private List<Child> child;
//setters and rest of class
}
@Entity
public class Child extends Common{
//some properties with getter/setters
}
@Entity
public class Address implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false)
private Long id;
private String street;
//rest of class with get/setter
}
在代码中,您可以看到父类和子类都继承了Common类,因此两者都具有地址属性和ID。问题出现在当更改父类中的地址引用时,它会反映在列表中的所有子对象中发生相同的更改,如果在子类中更改地址引用,则在合并时它也将更改父类的地址引用。
我无法确定这是JPA还是Hibernate的问题。