jpa update for objects having comosite primary key
-
26-09-2019 - |
Question
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?
Solution
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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow