我已经搜索了很长时间试图弄清楚这一点。我将 JPA 与 EclipseLink (Oracle DB) 一起使用。我有一个充满值的查找表。我有另一个与该表有 FK 关系的表。我可以很好地插入数据,但是当我尝试用不同的值更新表时,我得到了一个异常。我试过设置 CASCADE_TYPE 但这没有任何影响。我认为这很简单,但也许我错过了一些东西。
查找表:
public class SomeType implements Serializable {
@Id
@Column(name = "ID")
private Short id;
@Column(name = "TYPE")
private String type;
:
(getters & setters)
}
内容:
ID Type
------------
1 Type1
2 Type2
3 Type3
: :
人员表(为简洁起见,我省略了 Sequencing 的内容):
public class Person implements Serializable {
@Id
@Column(name = "ID")
private Short id;
@JoinColumn(name = "SOME_TYPE", referencedColumnName = "ID")
@ManyToOne(optional = false)
private SomeType someType;
:
(getters & setters)
}
插入工作正常:
EntityManager em;
:
Person p = new Person();
p.setSomeType(new SomeType(1));
em.persist(p);
这导致:
ID SOME_TYPE
------------------
1 1
但是如果我想更新 Person 来改变类型:
EntityManager em;
:
Person p = em.find(1);
SomeType newtype = new SomeType(2);
p.setSomeType(newtype);
em.merge(p);
我看到以下异常:
Exception [EclipseLink-7251] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [id] of class [SomeType] is mapped to a primary key column in the database. Updates are not allowed.
我想要的是要更新的 Person 表中的值,例如:
UPDATE PERSON set SOME_TYPE = 2 where ID = 1;
任何帮助将不胜感激。谢谢。