JPA ManyToMany:通过ID列表(而不是实际对象的列表)设置/更新 [英] JPA ManyToMany: set/update by list of ids instead of list of actual objects

查看:187
本文介绍了JPA ManyToMany:通过ID列表(而不是实际对象的列表)设置/更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个域类,它们之间具有许多2-许多关系.用户和组.数据库中有一个user2group表来映射这种关系.如果我想基于组ID列表(例如,可能来自HTML表单)来设置用户组,我是否真的必须先获取组才能通过setUsers(Set<Users>)更新用户?我已经有了组ID,而它们实际上是更新映射表所需的唯一信息...

I have two domain classes with a many-2-many relationship between them, e.g. User and Group. There's a user2group table in the database to map this relationship. If I want to set User's Groups based on a list of Group IDs (which might come e.g. from a HTML form) do I really have to fetch the Groups first to update the User through setUsers(Set<Users>)? I already have the Group IDs and they are actually the only information needed to update the mapping table...

我正在使用Spring Data JPA和Hibernate作为提供程序.

I'm using Spring Data JPA and Hibernate as the provider.

推荐答案

您可以编写本机查询,但是ORM的全部目的是使用它.如果关系的拥有方在用户上,则可能不必获取组,但是在组上进行定义是最常见的.在这种情况下,如果要使用ORM,是的,必须获取组.

You can write a native query, but the whole point of ORM is to use it. You may not have to fetch the Group if the owning side of the relationship is on the User, but it's most common to define it on the Group. In this case, if you are going to use the ORM, yes, you have to fetch the Groups.

这篇关于JPA ManyToMany:通过ID列表(而不是实际对象的列表)设置/更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆