具有组合主键的对象的 jpa 更新

发布于 2024-09-25 08:22:10 字数 363 浏览 0 评论 0原文

我的对象有一个复合主键。如何使用 jpa 更新我的对象?

通常我们使用以下代码

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();

现在如果我有一个主键是复合的对象那么我如何实现更新?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2024-10-02 08:22:10

几乎相同,只是您必须构造主键对象:

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

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文