spring-bootcomposite-keydatabase-sequence

Consuming sequence inside a embeddedId in springboot


I have the next issue -> I have a table on my db with a composite id... Supose (Id1,Id2,Id3,Id4), the Id4 is generated by a sequence on the db...

My question is, in spring boot, I generate the entity 'Table1' and the corresponding 'Table1Id', but when i want to add the corresponding GeneratedValue from the sequence, is not generating anything.

I was looking for in the internet and i found that the GeneratedValue is not working without the @Id anotation, but maybe there are some way to fix this issue.

Thank's and sorry for my english.


Solution

  • SOLVED:

    When a composite id is required in your project, it is impossible with an embeddedId. It is necesary to use @IdClass on my compossiteId instead @EmbeddedId, because the second does not work with @GeneratedValues for example my solution was:

    @Data
    @Entity(name = "table_name")
    @IdClass(CompositeIdTest.class)
    public class TestClass implements Serializable {
    
    
    
        @Id
        @Column(name = "column", nullable = false)
        private String column;
    
    
        @Id
        @SequenceGenerator(name = "sequence", sequenceName = "sequence", allocationSize = 1)
        @GeneratedValue(generator = "sequence")
        private int idGenerated;
    

    Anyway, thank's