org.hibernate.hql.ast.QuerySyntaxException:无法找到合适的构造函数

时间:2015-09-28 08:06:42

标签: hibernate constructor hql dto

我有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:无法在类[UserGroupsBean]上找到合适的构造函数

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

这是如何引起的?如何解决?

1 个答案:

答案 0 :(得分:1)

JPQL构造函数表达式仅支持平面结果,因此您运气不佳。您可以使用MIDDLEWARE_CLASSES = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', # MY MIDDLEWARE # 'config.middleware.EnforceLoginMiddleware', 'config.middleware.AuditMiddleware', 'config.middleware.ExceptionMiddleware', ] REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ), 'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',), 'PAGE_SIZE': 20 } [15/Feb/2017 15:57:16] "POST /api/public_end_point HTTP/1.1" 302 0 [15/Feb/2017 15:57:16] "GET /sign/base.html?nu=/api/public_end_point HTTP/1.1" 200 2906 之类的查询,但是您必须自己减少结果集。

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

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

这实际上是一个带有一些元数据的DTO。以下是查询代码:

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

    String getUserName();

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

这将生成类似于此

的查询
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();

并自动将结果映射到DTO。