views:

321

answers:

1

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?

+5  A: 

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).

Pascal Thivent
I SAID that i dont want to be updated...IT should be fixed...UPDATE ENTITYWITHENUM SET EXAMTYPE = ? WHERE (ID = ?) bind => [B, 1]should never be called...You are telling me how to update? I dont want the value to be updated...sorry
Parhs
@Parhs I'm sorry but I answered what I understood from your question. Maybe your question was not CLEAR or POORLY asked. But I think I got it now. Let me update my answer.
Pascal Thivent
I amnt a native English speaker:(Anyways... I want to make a specific collumn read only, but insertable.Because of the bug... insertable=false and updatable=false would only work.But setting them false i wont be able to persist!!So only these solutions come to my mind...1)Find by id and then update manually the attached entity..2)Create a custom update query (this seems poor to me)
Parhs
@Parhs I figured that out and that's ok (that little space in "how ever" was very confusing). But there is no need to shout (*"I SAID"*), I'm just trying to help you know. No problem anyway.
Pascal Thivent
Ok thank you! I think that version 2.02 is released...I was pissedof with eclipselink..
Parhs