Hibernate条件查询使用包装器类来选择特定列:没有合适的构造函数

时间:2018-06-09 06:13:34

标签: hibernate jpa constructor wrapper criteria

我想使用包装类从宽表中获取两个特定列,如下所示:https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#criteria-typedquery-wrapper

以下是来自hibernate.log的错误消息:

2018-06-09 07:52:08,119 (FromElement.java:147) FromClause{level=1} : myproject.oo.MyUser (generatedAlias0) -> myuser0_
2018-06-09 07:52:08,121 (FromReferenceNode.java:60) Resolved : generatedAlias0 -> myuser0_.uid
2018-06-09 07:52:08,122 (DotNode.java:688) getDataType() : uid -> org.hibernate.type.IntegerType@37c7595
2018-06-09 07:52:08,122 (FromReferenceNode.java:60) Resolved : generatedAlias0.uid -> myuser0_.uid
2018-06-09 07:52:08,123 (FromReferenceNode.java:60) Resolved : generatedAlias0 -> myuser0_.uid
2018-06-09 07:52:08,123 (DotNode.java:688) getDataType() : otherId -> org.hibernate.type.LongType@7d64e326
2018-06-09 07:52:08,123 (FromReferenceNode.java:60) Resolved : generatedAlias0.otherId -> myuser0_.otherID
2018-06-09 07:52:08,124 (ErrorCounter.java:60)  Unable to locate appropriate constructor on class [myProject.ProfileExtracter$MyWrapper]. Expected arguments are: int, long
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: myProject.ProfileExtracter$MyWrapper]
2018-06-09 07:52:08,125 (ErrorCounter.java:55)  Unable to locate appropriate constructor on class [myProject.ProfileExtracter$MyWrapper]. Expected arguments are: int, long
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: myProject.ProfileExtracter$MyWrapper]
org.hibernate.hql.internal.ast.DetailedSemanticException: Unable to locate appropriate constructor on class [myProject.ProfileExtracter$MyWrapper]. Expected arguments are: int, long

错误讯息:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [myProject.ProfileExtracter$MyWrapper]. Expected arguments are: int, long [select new myProject.ProfileExtracter$MyWrapper(generatedAlias0.uid, generatedAlias0.otherId) from myProject.oo.MyUser as generatedAlias0 ]

这是我的标准查询:

List<MyWrapper> selectPublicUsers_wrapper() {
    List<MyWrapper> list = null;
    Session session = HibernateUtil.getFactory(oo).openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();

        //create query
        CriteriaBuilder cb = session.getCriteriaBuilder();
        CriteriaQuery<MyWrapper> qry = cb.createQuery(MyWrapper.class);

        //FROM
        Root<MyUser> root = qry.from(MyUser.class);

        //SELECT
        qry.select(
                cb.construct(
                        MyWrapper.class,
                        root.get(MyUser_.uid),
                        root.get(MyUser_.otherId)
            )
        );

        //WHERE
        Predicate hasWhere = cb.equal(root.get(MyUser_.hasWhere), PUBLIC.toString());
        qry.where(hasWhere);

        list = session.createQuery(qry).setMaxResults(50).getResultList();
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
        log.error(e);
    } finally {
        if (session != null)
            session.close();
    }
    if (list == null || list.size() == 0) {
        list = new ArrayList<>();
    }
    return list;
}

MyWrapper.class。

public class MyWrapper implements java.io.Serializable {
    private Integer uid; //It is in fact Integer
    private Long otherId; //It is in fact Long

    public MyWrapper() {}

    public MyWrapper(Integer uid, Long otherId) { //seemingly unappropriate contructor
        this.uid = uid;
        this.otherId = otherId;
    }
}

我试过了:

  • 所有可能组合中的所有构造函数
  • Wrapper类:首先我将它实现为内部类。我也试过把它变成自己的类。
  • 尝试使用适当的构造函数只获取一列而不是两列。
  • 使包装类静态
  • 使用可以正常工作的Object []查询。我真的想使用包装类代替代码可读性和性能原因!

我仍然收到错误消息。我错过了什么?

0 个答案:

没有答案