我有这个代码
@Column(updatable=false)
@Enumerated(EnumType.STRING)
private ExamType examType;
但是,当我通过合并更新它时,我仍然可以更改该值。为什么?
我有这个代码
@Column(updatable=false)
@Enumerated(EnumType.STRING)
private ExamType examType;
但是,当我通过合并更新它时,我仍然可以更改该值。为什么?
行。首先,如果您希望该examType
列包含在SQL UPDATE
语句中,则不应使用updatable=false
. updatable=false
话虽如此,当不与它结合使用时似乎会被忽略,insertable=false
但这是 EclipseLink 中的一个错误(错误 243301),这不是 JPA 所说的。将其设置为true
或删除它。
其次,具有以下实体:
@Entity
public class MyEntity {
@Id
@GeneratedValue
private Long id;
@Column(updatable = true)
@Enumerated(EnumType.STRING)
private ExamType examType;
...
}
以下测试方法在 EclipseLink 上运行良好:
@Test
public void testUpdateOfEnum() {
MyEntity e = new MyEntity();
e.setExamType(ExamType.A);
em.persist(e);
em.flush();
assertNotNull(e.getId());
assertEquals(ExamType.A, e.getExamType());
e.setExamType(ExamType.B);
em.merge(e);
em.flush();
em.refresh(e); // to ensure we assert against value read from the db
assertEquals(ExamType.B, e.getExamType());
}
在生成的 SQL 语句下方:
插入到实体中(ID,EXAMTYPE)值(?,?) 绑定 => [1, A] UPDATE ENTITYWITHENUM SET EXAMTYPE = ? 哪里(ID =?) 绑定 => [B, 1] SELECT ID, EXAMTYPE FROM ENTITYWITHENUM WHERE (ID = ?) bind => [1]
Honestly, a bug on such an elementary thing in EclipseLink is very unlikely, more than a mistake on your side if I may :)
Update: After reading the comment from the OP, I think I got the question now (which was totally unclear to be honest): the OP actually doesn't want the examType
to be updated which is the exact opposite of my initial understanding. So the OP is actually facing Bug 243301 (fix to be released in 2.0.2):
EclipseLink only allows mappings to be writable or read-only. Mappings marked as both insertable=false and updatable=false will be set as read-only.
Another workaround is described in Bug 294803 (that the previous one duplicates).