gpt4 book ai didi

java - 当有manytomany注解时Spring data jpa投影重复输出

转载 作者:行者123 更新时间:2023-11-30 06:19:27 25 4
gpt4 key购买 nike

我有一个与权限类相关的用户类,它们之间具有多对多关系。

我正在使用投影来检索没有某些字段的用户列表。当我检索用户拥有的权限列表时出现问题,对于每个权限,spring 都会向我返回相同的对象两次。

用户类别

@Entity
@Table(name = "user")
public class User {


@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "user_seq")
@SequenceGenerator(name = "user_seq", sequenceName = "user_seq", allocationSize = 1)
private Long id;

@Column(name = "apiKey", updatable = false, nullable = false, unique=true, columnDefinition = "BINARY(16)")
private UUID apiKey;


@Column(name = "USERNAME", length = 50, unique = true)
@NotNull
@Size(min = 4, max = 50)
private String username;

@Column(name = "PASSWORD", length = 100)
@NotNull
@Size(min = 4, max = 100)
private String password;

@Column(name = "FIRSTNAME", length = 50)
@NotNull
@Size(min = 4, max = 50)
private String firstname;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "USER_AUTHORITY",
joinColumns = {@JoinColumn(name = "USER_ID", referencedColumnName = "ID")},
inverseJoinColumns = {@JoinColumn(name = "AUTHORITY_ID", referencedColumnName = "ID")})
private List<Authority> authorities;


}

用户存储库

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);

List<UserWithUuid> findAllByNodeId(Long nodeId);

interface UserWithUuid {
String getFirstname();
String getLastname();
String getEmail();
List<Authority> getAuthorities();
UUID getApiKey();
}



}

正如您在输出中看到的,它返回两次相同的用户,但具有不同的角色。

输出

[
{
"firstname": "daniel",
"lastname": "mancera",
"email": "daniel.mancera@dmance.eu",
"apiKey": "7961609f-79d4-4ef2-9baa-6809978c038b",
"authorities": [
{
"id": 1,
"name": "ROLE_USER"
}
]
},
{
"firstname": "daniel",
"lastname": "mancera",
"email": "daniel.mancera@dmance.eu",
"apiKey": "7961609f-79d4-4ef2-9baa-6809978c038b",
"authorities": [
{
"id": 2,
"name": "ROLE_MANAGER"
}
]
},
{
"firstname": "Roger",
"lastname": "Rabbit",
"email": "roger.rabiit@rab.it",
"apiKey": "023bf60b-e79b-461f-bd30-65a920fe99e4",
"authorities": [
{
"id": 1,
"name": "ROLE_USER"
}
]
}
]

最佳答案

这很可能是这个 Bug https://jira.spring.io/browse/DATAJPA-1173

它已在当前版本的 Spring Data 中修复,因此升级您使用的版本应该可以解决该问题。

关于java - 当有manytomany注解时Spring data jpa投影重复输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48502670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com