public List getCity() {   //方法
// TODO Auto-generated method stub
session = HibernateSessionFactory.getSession();
List list  =  session.createQuery("select new com.snzo.dao.WorldOnlinePortCity(wp.id,wp.name) from WorldOnlinePortCity as wp").list();    //用过直接 new WorldOnlinePortCity(wp.id,wp.name)
session.close();
return list ;
} public static void main(String []args){  测试 
List list = new City_Imp().getCity();
for(Iterator it = list.iterator();it.hasNext();){
WorldOnlinePortCity goods = (WorldOnlinePortCity)it.next();
System.out.println(goods.getId());
}
}
      
    public class WorldOnlinePortCity implements java.io.Serializable {   //持久化类 // Fields private Long id;       
private String name;
private String des;  
private Set city;
// Constructors /** default constructor */
public WorldOnlinePortCity() {  //默认构造
} /** minimal constructor */
// public WorldOnlinePortCity(Long id) {
// this.id = id;
// } /** full constructor */
public WorldOnlinePortCity(Long id, String name, String des,Set city) {   //构造函数
this.id = id;
this.name = name;
this.des = des;
this.city=city;    //Set 类型 不写也试验过 报一样的错。
}   不管 怎么写 都这这样的错误  
  错误:
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.snzo.dao.WorldOnlinePortCity] [select new com.snzo.dao.WorldOnlinePortCity(wp.id,wp.name) from com.snzo.dao.WorldOnlinePortCity as wp]
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:225)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:158)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
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:1583)
at com.snzo.bo.City_Imp.getCity(City_Imp.java:75)
at com.snzo.bo.City_Imp.main(City_Imp.java:81)
Caused by:  Unable to locate appropriate constructor on class [com.snzo.dao.WorldOnlinePortCity]
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: com.snzo.dao.WorldOnlinePortCity]
at org.hibernate.hql.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:122)
at org.hibernate.hql.ast.tree.ConstructorNode.prepare(ConstructorNode.java:88)
at org.hibernate.hql.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:837)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:1952)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1821)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1392)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:553)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:218)
... 10 more
    // 在线查了很多资料 依然没有找到原因 在线等 高手 解答 谢谢.

解决方案 »

  1.   

    你这个WorldOnlinePortCity 类中没有2个参数的构造方法啊
    而你的hql语句只给了2个参数。。
    select new com.snzo.dao.WorldOnlinePortCity(wp.id,wp.name) from WorldOnlinePortCity as wp
      

  2.   

      哥们  I服了YOU 就是这个原因 如果有时间 能把这个原因 简单的说下嘛 或者 有什么网址什么的 给我留个 我工作不长 对于hibernate不是特别了解 所以 请多指教 O(∩_∩)O谢谢!!!
      

  3.   

    请问楼主此问题解决没,我今天也碰到了此类问题提示是org.hibernate.PropertyNotFoundException: no appropriate constructor in class,查询几个字段我就写了相应字段个数的构造函数,都不好使。
      

  4.   

     楼主,请求帮助sql语句如下
    String smsSql = "select new CupBill(appID,serviceCode,wayID,count(ID)) from CupSmsMT where sentDate>=? and sentDate<=? and receipt=0 group by appID,serviceCode,wayID";
    构造方法如下:
    public CupBill(int appID, String serviceCode, int wayID,
    int quantity) {
    super();
    this.appID = appID;
    this.serviceCode = serviceCode;
    this.wayID = wayID;
    this.quantity = quantity;
    }我不知道是不是因为查询的名称和构造方法的名称不一样还是什么其他的问题,和你报同样的错误
      

  5.   

    类 WorldOnlinePortCity 要有WorldOnlinePortCity (Long id, String name)这种两个参数的构造方法, new
    WorldOnlinePortCity ()的时候才会成功