简体   繁体   中英

JPA 2.0 Eclipse Link

I have this code

@Column(updatable=false)
@Enumerated(EnumType.STRING)
private ExamType examType;

However, I can still change the value when I update it via merge. Why?

Ok. First, if you want the examType column to be included in SQL UPDATE statements, you shouldn't mark it with updatable=false . That being said, it appears that updatable=false is ignored when not used in combination with insertable=false but that's a bug in EclipseLink ( Bug 243301 ), that's not what JPA says. Set it to true or remove it.

Secondly, with the following entity:

@Entity
public class MyEntity {
    @Id
    @GeneratedValue
    private Long id;

    @Column(updatable = true)
    @Enumerated(EnumType.STRING)
    private ExamType examType;

    ...
}

The following test method just runs fine with 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());
}

Below the generated SQL statements:

INSERT INTO ENTITYWITHENUM (ID, EXAMTYPE) VALUES (?, ?)
    bind => [1, A]
UPDATE ENTITYWITHENUM SET EXAMTYPE = ? WHERE (ID = ?)
    bind => [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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM