I have a table (not created by me and it cannot be modified by me either) with ~30 columns with a surrogate primary key from a sequence. From these 30 columns, only 3 are not nullable (let's call them col1, col2 and col3) and using these 3 and other 6 nullable (col4 ... col9) columns I have an unique index. How can I map this with Hibernate since it doesn't allow null values at @Id
attributes nor @EmbeddedId
? What is the best workaround recommended considering I'm not allowed to change any database property?
Consider I want to implement a crud rest api that should check if that entity is already present to update data, otherwise, insert it inside the same method. Passing (1,2,3) values (col1,col2,col3)
to my query should result into a successful insert with an empty table. After that, if I try to insert (1,2,3,4) values (col1,col2,col3,col4)
and the forth value is from a column indexed by previous unique index, it should insert another row, resulting into:
seqPK col1(notNull) col2(notNull) col3(notNull) col4(null) col25(null) ...
1 1 2 3 null null ...
2 1 2 3 4 null ...
Because (1,2,3,null(x6)) is different from (1,2,3,4,null(x5)). But if I try to insert (1,2,3,25) values (col1,col2,col3,col25)
and the forth value is from a non indexed column, it should update the first inserted row like this:
seqPK col1 col2 col3 col4(null) col25(null) ...
1 1 2 3 null 25 ...
2 1 2 3 4 null ...
I'm planning using a @Query
at my findById(passing all 9 arguments) and always forcing them to be not null (@NotNull
), thus forcing uniqueness from their attributes without using any @Id
functionalities hibernate could me provide. Is there a better approach?