JPA, how to handle a table with a composite key (String, Int) where the int portion is assigned by DB trigger
I have a table with composite primary key, one of the PK fields is a String (group) and the other an integer (id), which is assigned by a trigger (more less auto incrementing but it will start counting from 1 for each new string).
I can read, write, update... but not create more than one entity (the constructor sets the int part of the PK to 0, which works fine the trigger will overwrite the value but it would be best if it does not pass anything in.)
I think it would be nice if I could make the id portion read only, or ... I've 开发者_高级运维noticed from looking at the generated SQL that it is setting every value... is it possible to exclude trying to set a value if it is null? That would get the value written.
For a quick hack to resolve this, I just added a static member to the entity class which is used for the key component. I decided to go with negative numbers, just to keep clear what is an 'insert only' entity which should be thrown away after insert and the positive valued ones which would have been retrieved from the db. Messy but works.
精彩评论