gpt4 book ai didi

hibernate - org.hibernate.hql.ast.QuerySyntaxException : Unable to locate appropriate constructor

转载 作者:行者123 更新时间:2023-12-04 00:12:16 31 4
gpt4 key购买 nike

我有 2 个表映射到实体 UserGroup .

@Entity
@Table(name = "T_COM_USER")
public class User {
private String userName;
@Column(name="F_USERNAME", length=60)
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
@Entity
@Table(name="T_COM_USERGROUP")
public class UserGroup{
private String groupName;
@Column(name="F_GROUPNAME", length=60)
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
}

一个User可以有很多 Group秒。我需要对它们进行建模,以便在前端的表格中显示用户及其所有组,如下所示:

+-------+----------------------+
| Users | Groups |
+-------+----------------------+
| User1 | Group1,Group2,Group3 |
| User2 | Group1,Group2 |
| User3 | Group2,Group4 |
+-------+----------------------+

所以我创建了这个 DTO:

public class UserGroupsBean {
private List<String> groupName;
private String userName;
public UserGroupsBean(String userName, List<String> groupName) {
this.userName = userName;
this.groupName = groupName;
}
// Getters
public List<String> getGroupName() { return groupName; }
public String getUserName() { return userName; }
// Setters
public void setGroupName(List<String> groupName) { this.groupName = groupName; }
public void setUserName(String userName) { this.userName = userName;}
}

我使用此查询返回每个用户的所有组:

String hql = "select new odatabase.service.beans.UserGroupsBean(userName,(select groupName from UserGroup) ) from User";

但是我得到了:

org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [UserGroupsBean]

虽然我有一个构造函数UserGroupsBean(String userName, List < String > groupName)

这是怎么引起的,我该如何解决?

最佳答案

JPQL 构造函数表达式只支持平面结果,所以你运气不好。您可以使用类似 SELECT u.userName, g.groupName FROM User u JOIN u.userGroups g 的查询,但您必须自行减少结果集.

Blaze-Persistence Entity Views 可以很好地完成您想做的事情.这里有一些示例代码。

@EntityView(User.class)
public interface UserGroupsBean {
// The id of the user
@IdMapping("id") int getId();

String getUserName();

@Mapping("userGroups.name")
List<String> getGroupNames();
}

这本质上是一个带有一些元数据的 DTO。下面是查询代码:

EntityManager entityManager = // jpa entity manager
CriteriaBuilderFactory cbf = // query builder from Blaze-Persistence
EntityViewManager evm = // manager that can apply entity views to query builders

CriteriaBuilder<User> builder = cbf.create(entityManager, User.class);
List<UserGroupsBean> result = evm.applySetting(
builder,
EntityViewSetting.create(UserGroupsBean.class)
).getResultList();

这将生成一个类似于此的查询

SELECT u.id, u.userName, g.groupName FROM User u JOIN u.userGroups g

并会自动将结果映射到 DTO 中。

关于hibernate - org.hibernate.hql.ast.QuerySyntaxException : Unable to locate appropriate constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32818321/

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