多对多 - select 没有确定字段且结果数量有限的实体集合
Many to many - select collection of entities without determined field and with limited number of results
我有两个实体class存在多对多关系
@Entity
public class User implements Serializable {
...
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "members")
private Set<Group> groups = new HashSet<>();
}
和
@Entity
public class Group implements Serializable {
...
@JoinTable(name = "user_groups",
joinColumns = {@JoinColumn(name = "group_id")},
inverseJoinColumns = {@JoinColumn(name = "user_id")})
private Set<User> members = new HashSet<>();
}
当我请求获取特定组时,我得到:
{
"id": 12,
"adminId": 12345,
"name": "name",
"category": "cat",
"city": "city",
"members": [
{
"id": 1,
"firstName": "SomeName",
"birthday": 802476000000,
"city": "SomeCity",
"country": "SomeCountry",
"age": 21,
"groups": [
{
"id": 1,
"adminId": 123,
"name": "Some group name",
"category": "some category",
"city": "some city"
}
],
"picturePath": some path
}
]
}
当我想通过 id 获取特定组时,在其成员字段中,我只想拥有没有组字段的用户和数据库中的前 100 行。
希望您理解:用户属于多个组,组有多个用户
我想要这样的东西:
{
"id": 12,
"adminId": 12345,
"name": "name",
"category": "cat",
"city": "city",
"members": [
{
"id": 1,
"firstName": "SomeName",
"birthday": 802476000000,
"city": "SomeCity",
"country": "SomeCountry",
"age": 21,
"picturePath": some path
}
]
我想到的是创建和连接 table 的实体 class(其中包含列:user_id、group_id)并创建将获得的查询给定 group_id 的 user_id 的结果,然后在循环中让用户获得我感兴趣的字段
也许我应该使用一些元组查询?
感谢您花时间解决我的问题
哦,愚蠢的我:)
Query query = entityManager.createNativeQuery("SELECT u.user_id,u.birthdate,u.first_name,u.city,u.country,u.age,u.picture_path " +
"FROM users u " +
"INNER JOIN user_groups ug " +
"ON ug.user_id = u.user_id WHERE ug.group_id = ?",User.class);
user_groups 是一个连接 table,可以使用 LIMIT 来计算结果(在 MySQL 中)
我有两个实体class存在多对多关系
@Entity
public class User implements Serializable {
...
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "members")
private Set<Group> groups = new HashSet<>();
}
和
@Entity
public class Group implements Serializable {
...
@JoinTable(name = "user_groups",
joinColumns = {@JoinColumn(name = "group_id")},
inverseJoinColumns = {@JoinColumn(name = "user_id")})
private Set<User> members = new HashSet<>();
}
当我请求获取特定组时,我得到:
{
"id": 12,
"adminId": 12345,
"name": "name",
"category": "cat",
"city": "city",
"members": [
{
"id": 1,
"firstName": "SomeName",
"birthday": 802476000000,
"city": "SomeCity",
"country": "SomeCountry",
"age": 21,
"groups": [
{
"id": 1,
"adminId": 123,
"name": "Some group name",
"category": "some category",
"city": "some city"
}
],
"picturePath": some path
}
]
} 当我想通过 id 获取特定组时,在其成员字段中,我只想拥有没有组字段的用户和数据库中的前 100 行。 希望您理解:用户属于多个组,组有多个用户
我想要这样的东西:
{
"id": 12,
"adminId": 12345,
"name": "name",
"category": "cat",
"city": "city",
"members": [
{
"id": 1,
"firstName": "SomeName",
"birthday": 802476000000,
"city": "SomeCity",
"country": "SomeCountry",
"age": 21,
"picturePath": some path
}
]
我想到的是创建和连接 table 的实体 class(其中包含列:user_id、group_id)并创建将获得的查询给定 group_id 的 user_id 的结果,然后在循环中让用户获得我感兴趣的字段
也许我应该使用一些元组查询? 感谢您花时间解决我的问题
哦,愚蠢的我:)
Query query = entityManager.createNativeQuery("SELECT u.user_id,u.birthdate,u.first_name,u.city,u.country,u.age,u.picture_path " +
"FROM users u " +
"INNER JOIN user_groups ug " +
"ON ug.user_id = u.user_id WHERE ug.group_id = ?",User.class);
user_groups 是一个连接 table,可以使用 LIMIT 来计算结果(在 MySQL 中)