hibernate中PO的构造器顺序

构造器的顺序需要和hql中的顺序一致。否则会出现如下错误:

Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.flyinsky.eagle.user.po.UserInforPO] [select new UserInforPO(carId,name) from com.flyinsky.eagle.user.po.UserInforPO where name = ? and password = ?]

atorg.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)

atorg.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:225)

atorg.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:158)

atorg.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)

atorg.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:75)

atorg.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:54)

atorg.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)

atorg.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)

atorg.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)

atorg.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)

atcom.flyinsky.eagle.user.dao.UserDAO.getAllUsers(UserDAO.java:263)

atcom.flyinsky.eagle.user.test.TestHibernate.main(TestHibernate.java:69)

Causedby:Unabletolocateappropriateconstructoronclass[com.flyinsky.eagle.user.po.UserInforPO]

[cause=org.hibernate.PropertyNotFoundException:noappropriateconstructorinclass:com.flyinsky.eagle.user.po.UserInforPO]

atorg.hibernate.hql.ast.tree.ConstructorNode.resolveConstructor(ConstructorNode.java:122)

atorg.hibernate.hql.ast.tree.ConstructorNode.prepare(ConstructorNode.java:88)

atorg.hibernate.hql.ast.HqlSqlWalker.processConstructor(HqlSqlWalker.java:837)

atorg.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:1952)

atorg.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1821)

atorg.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1392)

atorg.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:553)

atorg.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)

atorg.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)

atorg.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:218)

 ... 10 more
public class UserInforPO {
	private int carId;
	private String name;
	private int age;
	private String password;
	private Date regDate;
	private List<UserAddressPO> allAddress = new ArrayList<UserAddressPO>();

	private String email;

	public UserInforPO() {

	}

	public UserInforPO(String name,int carId) {
		this.name = name;
		this.carId = carId;
	}

	public UserInforPO(String name, int age, List address) {
		this.name = name;
		this.allAddress = address;
		this.age = age;
	}
public void getAllUsers(UserInforPO userPO){
		String hql = "select new UserInforPO(carId,name) from UserInforPO " +
				"where name = ? and password = ?";//安位置进行数值绑定
		Session session = DBUtil.getSession();
		Query query = session.createQuery(hql);
		query.setParameter(0, userPO.getName());
		query.setParameter(1, userPO.getPassword());
		
		List list = query.list();
		for(int i = 0; i < list.size(); i++){
			UserInforPO user = (UserInforPO)list.get(i);
			System.out.println(user.getCarId() + "   " + user.getName());
		}

 将PO改为如下就可以了

public class UserInforPO {
	private int carId;
	private String name;
	private int age;
	private String password;
	private Date regDate;
	private List<UserAddressPO> allAddress = new ArrayList<UserAddressPO>();

	private String email;

	public UserInforPO() {

	}

	public UserInforPO(int carId,String name) {
		this.name = name;
		this.carId = carId;
	}

相关推荐