I have HQL query
我有HQL查询
select new PaymentType(o.paymentType.idPaymentType) from Order as o where o.user='1'
it throws following exception
它抛出异常
org.hibernate.PropertyNotFoundException: no appropriate constructor in class: PaymentType
at org.hibernate.util.ReflectHelper.getConstructor(ReflectHelper.java:187)
at org.hibernate.hql.classic.QueryTranslatorImpl.renderSQL(QueryTranslatorImpl.java:631)
at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:220)
at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:185)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
But I have declared required constructor, except of this, there are declared other tree constructors with different count and types of params, and also no params constructor.
但是我已经声明了必需的构造函数,除此之外,还声明了其他树构造函数具有不同的params数量和类型,也没有params构造函数。
public PaymentType(Integer idPaymentType) {
this.idPaymentType = idPaymentType;
}
Edit:
public class Order implements java.io.Serializable {
private Integer idOrder;
private PaymentType paymentType;
private DeliveryType deliveryType;
...
}
2 个解决方案
#1
0
two things worth to check. 1.try use a fully qualified name of PaymentType
有两件事值得一试。 1.try使用PaymentType的完全限定名称
select new com.company.xxx.PaymentType(o.paymentType.idPaymentType)
2.also make sure the id in entity Order is int/integer rather than Long or something else.
2.还要确保实体Order中的id是int / integer而不是Long或其他东西。
#2
0
I have seen this exception before working with Hibernate. From what I recall, it either has to do with not having the parameter-less constructor, the parameter-less constructor not having public acccess, or your entity class not being serializable.
在使用Hibernate之前,我已经看到了这个异常。根据我的记忆,它要么与没有参数的构造函数,没有参数的构造函数没有公共访问,或者你的实体类不可序列化。
Try the following:
请尝试以下方法:
-
Make sure you have these constructors in
PaymentType
class:确保在PaymentType类中有这些构造函数:
/** * Default, parameter-less constructor */ public PaymentType() { }
-
And also for
Order
:还有订单:
/** * Default, parameter-less constructor */ public Order() { }
-
Make sure that both
PaymentType
andOrder
implementjava.io.Serializable
.确保PaymentType和Order都实现了java.io.Serializable。
#1
0
two things worth to check. 1.try use a fully qualified name of PaymentType
有两件事值得一试。 1.try使用PaymentType的完全限定名称
select new com.company.xxx.PaymentType(o.paymentType.idPaymentType)
2.also make sure the id in entity Order is int/integer rather than Long or something else.
2.还要确保实体Order中的id是int / integer而不是Long或其他东西。
#2
0
I have seen this exception before working with Hibernate. From what I recall, it either has to do with not having the parameter-less constructor, the parameter-less constructor not having public acccess, or your entity class not being serializable.
在使用Hibernate之前,我已经看到了这个异常。根据我的记忆,它要么与没有参数的构造函数,没有参数的构造函数没有公共访问,或者你的实体类不可序列化。
Try the following:
请尝试以下方法:
-
Make sure you have these constructors in
PaymentType
class:确保在PaymentType类中有这些构造函数:
/** * Default, parameter-less constructor */ public PaymentType() { }
-
And also for
Order
:还有订单:
/** * Default, parameter-less constructor */ public Order() { }
-
Make sure that both
PaymentType
andOrder
implementjava.io.Serializable
.确保PaymentType和Order都实现了java.io.Serializable。