ids for this class must be manually assigned before calling save()

后端 未结 3 682
情书的邮戳
情书的邮戳 2021-01-02 17:38

I\'ve got some problem with hibernate @OneToMany mapping. It goes like here

@Entity
@Table(name = \"albums\")
@SequenceGenerator(name = \"ALBUMS         


        
相关标签:
3条回答
  • 2021-01-02 18:14

    I was facing the same problem, but what worked for me was

    B oB = new B();        // Class B contains two objects of class A
    
    A oA1 = new A();
    oA1.Name = "First";
    oB.PlaceA1 = oA1;
    
    A oA2 = new A();
    oA2.Name = "Second";
    oB.PlaceA2 = oA2;
    

    Changed to

    B oB = new B();        // Class B contains two objects of class A
    
    A oA1 = new A();
    oA1.ID = 1;            // This was missing(Manual Assignment of ID field)
    oA1.Name = "First";
    oB.PlaceA1 = oA1;
    
    A oA2 = new A();
    oA2.ID = 2;            // This was missing(Manual Assignment of ID field)
    oA2.Name = "Second";
    oB.PlaceA2 = oA2;
    
    0 讨论(0)
  • 2021-01-02 18:26

    You are using two primary keys in one table and one of the keys is also a primary key in another table. The simple solution is to remove the one of the primary keys which is also the foreign key

    0 讨论(0)
  • 2021-01-02 18:31

    First - it can't generate them because you didn't tell it how to do it. Use @GeneratedValue and choose the strategy you want.

    Then - you can't have two @Id fields. If you want a composite id use @EmbeddedId or @IdClass

    0 讨论(0)
提交回复
热议问题