Search code examples
javajpadb2openjpa

OpenJPA produces incorrect query despite an annotation


I have an hierarchy of buildings/floors/rooms in my application. Whenever I'm trying to add the floor (with entiryManager.persist(floor)) I get the following exception:

Caused by: <openjpa-2.2.3-SNAPSHOT-r422266:1715851 fatal general error> org.apache.openjpa.persistence.PersistenceException: DB2 SQL Error: SQLCODE=-798, SQLSTATE=428C9, SQLERRMC=FLOOR_ID, DRIVER=3.68.61 {prepstmnt 925512112 INSERT INTO ROOMALLOCATION.Floor (FLOOR_ID, FLOOR_NAME, FLOOR_CODE, SITE_ID) VALUES (?, ?, ?, ?)} [code=-798, state=428C9]SQLCA OUTPUT[Errp=SQLRI079, Errd=-2145779603, 0, 0, 0, -95, 0]

DB2 SQL Error: SQLCODE=-798, SQLSTATE=428C9, SQLERRMC=FLOOR_ID, DRIVER=3.68.61 FailedObject: com.RoomAllocation.entities.Floor@1d32457

On the floor object, specifically FLOOR_ID:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "FLOOR_ID")
private int floorId;

The field in database is GENERATED ALWAYS AS IDENTITY.

Same setup works for buildings and rooms, I used the same code to create and persist objects. As far as I understand openJPA supposed to put DEFAULT in the query, instead of this it's putting 0, because it's int default value. Is there a way to fix it? Something I'm missing?


Solution

  • By adding insertable = false, updatable = false to @Column annotation I succeeded to stop this behavior.