views:

36

answers:

1

I have a composite primary key for my object.How can i use a jpa to update my object?

Normally we use the following code

EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
EntityManager em = emf.createEntityManager();
Student stud1 = em.find(Student.class,1);
stud1.setSname("Deepak");
//set others
em.merge(stud1);
em.getTransaction().commit();

Now if i have a object whose primary key is composite then how can i implement update?

+2  A: 

Pretty much the same, except that you'll have to construct the primary key object:

Student stud1 = em.find(Student.class, new StudentPK(pkPart1, pkPart2));
Bozho