0

User Table

UserName pk fname lname

GroupMaster

GroupName pk UserName pk, fk

I want to insert data in GroupMaster but my problem is it has composite primary key

How can I insert data in table through entity manager object?

bd33
  • 502
  • 1
  • 15
  • 30
Hirren Gamit
  • 99
  • 2
  • 3
  • 10

1 Answers1

1

This can be done with the @Embeddable annotation. Set up a new class that contains the values of the primary key -- in this case, a class with GroupName and UserName fields, annotate the class with @Embeddable, then store an instance inside the @Entity that requires the primary key.

Take a look at the following example:

Using composite keys in Hibernate

Apropos
  • 508
  • 5
  • 15
  • I already have class but i want the exact way to insert data like em.persist(whichobject) – Hirren Gamit Dec 31 '12 at 18:41
  • @user1904230 - There should be two classes -- one is your `@Entity`, one is your `@Embeddable`. You would persist the `@Entity`. – Apropos Dec 31 '12 at 19:25