我想使用包装类从宽表中获取两个特定列,如下所示: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;
}
}
我试过了:
我仍然收到错误消息。我错过了什么?