我有两个实体 - 超类及其子类:
超类(但不是抽象):
@Entity
@Table(name="table1")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class table1 implements Serializable {
@Id
String id;
}
子类:
@Entity
@Table(name="table2")
public class table2 extends table1 {
String somefield;
}
两个表在启动时都可以正常创建。
但是当我试图保存子类的对象时:
table2 obj = new table2();
dbSession.save(obj);
我收到错误:org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session
如何解决这个问题?我只需要从第一个类扩展第二个类,并将其用作不同的表。
我也尝试了.merge()
方法,但它仍然无效。
答案 0 :(得分:2)
你有String ID,当你持久化对象时,你确定它不是空的或者你的数据库中不存在吗?
试试这个:
table2 obj = new table2();
obj.setId("new unique id")
dbSession.save(obj);