gpt4 book ai didi

java - JPA:@SqlResultSetMapping 给了我两个对象,而不是一个

转载 作者:行者123 更新时间:2023-12-01 05:42:56 25 4
gpt4 key购买 nike

我正在尝试执行一些 NativeQueries,但我坚持使用 @SqlResultSetMapping 配置。

我有这些类(class):

@Entity  
public class LocalUser implements Serializable {
private static final long serialVersionUID = 1265845L;
@Id
private String username;
@Column(nullable=false)
private String password;

@OneToMany
private List<RoleName> roles;

public LocalUser() {
}

public LocalUser(String username, String password, List<RoleName> roles) {
this.username = username;
this.password = password;
this.roles = roles;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public List<RoleName> getRoles() {
return roles;
}

public void setRoles(List<RoleName> roles) {
this.roles = roles;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
}

@Entity
public class RoleName implements Serializable {
private static final long serialVersionUID = 12598741564894L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String roleName;

public RoleName() {
}

public RoleName(Long id, String roleName) {
this.id = id;
this.roleName = roleName;
}

public RoleName(String roleName) {
this.roleName = roleName;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getRoleName() {
return roleName;
}

public void setRoleName(String roleName) {
this.roleName = roleName;
}
}

我的问题是,如何为 LocalUser 类编写适当的 @SqlResultSetMapping 。我的测试SQL语句是:

select 'user' as username, 'passwd' as password, 1 as id, 'admin' as roleName;  

我想获得一个实体本地用户,其用户名 user、密码 passwd 和角色列表,其中 id 为 1 的 admin。

我认为到目前为止是这样的:

@SqlResultSetMapping(name="LocalUserMapping",  
entities={
@EntityResult(entityClass=LocalUser.class,fields={
@FieldResult(name="username",column="username"),
@FieldResult(name="password",column="password")
}),
@EntityResult(entityClass=RoleName.class,fields={
@FieldResult(name="id",column="id"),
@FieldResult(name="roleName",column="roleName")
})
})

但它给了我两个对象,而不是一个。有人可以帮我吗?谢谢。

最佳答案

这两个对象是使用 entityClass= 注释产生的(2x,一个用于 LocalUser,一个用于 RoleName)。将它们组合成一个entityClass定义并返回一个对象。

关于java - JPA:@SqlResultSetMapping 给了我两个对象,而不是一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6683689/

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