0

i try to create composite primary key in table from 2 foreign key using hibernate.but i do not now the way how to set it. @UniqueConstraint can some body help me..

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Niraj Thakar
  • 168
  • 1
  • 2
  • 13
  • Have you tried something, some tutorial? Can you post your entity class where you want to have composite PK? You should not need `@UniqueConstraint`. – Ondrej Bozek Aug 01 '13 at 07:52

1 Answers1

0

you don't need @UniqueConstraint if you declare that composite key as your @EmbeddedId see this link for the example.

you only need a class to become it's composite id:

@Embeddable
public class SomeCompositeClass {
    @Column(name = "SOME_ID_1")
    private String someId1;

    @Column(name = "SOME_ID_2")
    private String someId2;

    //getter, setter methods
}

then use it inside your entity to became its @EmbeddedId :

 @Entity
 @Table(name = "YOUR_MAIN_TABLE")
 public class YourMainTable {

     @EmbeddedId
     private SomeCompositeClass myCompositeId ;

  /*setter getter methods */
 }
Community
  • 1
  • 1
Angga
  • 2,305
  • 1
  • 17
  • 21