日期:2014-05-18  浏览次数:20956 次

一百分求解,初写的一个HIBERNATE问题
总是错误提示
net.sf.hibernate.QueryException: undefined alias: id [select id from meis.hibern 
ate.BULLETIN as BULLETIN ] 

代码
代码 
SessionFactory sf = new Configuration().configure().buildSessionFactory(); 
//打开一个Session 
Session session = sf.openSession(); 
//开始一个事务 
Transaction tx = session.beginTransaction(); 
   
Query query=session.createQuery("select id from BULLETIN as BULLETIN "); 
  
query.list(); 



映射类
package meis.hibernate;

public class BULLETIN 
{
//发布日期

private String id;

private String issuedate;
private String title;

public String getId()

return id; 

public void setId(String id) 

this.id = id; 
}

public String getIssuedate()

return issuedate; 

public void setIssuedate(String issuedate) 

this.issuedate = issuedate; 


public String getTitle()

return title; 

public void setTitle(String title) 

this.title = title; 
}

}


配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
  <class name="meis.hibernate.BULLETIN" table="BULLETIN">
  <id name="id">
  <generator class="assigned" />
  </id>
  <property name="issuedate" /> 
  <property name="title" /> 
  </class> 

</hibernate-mapping>


------解决方案--------------------
Query query=session.createQuery("select id from BULLETIN");

不用as,否则的话,必须是:
select BULLETIN.id from BULLETIN as BULLETIN
------解决方案--------------------
select id from BULLETIN as BULLETIN
改为:
select BULLETIN.id from BULLETIN as BULLETIN
最好不要用与类相同的别名,
建议改成以下的方式:
select bull.id from BULLETIN as bull
------解决方案--------------------
好像你语句都已经正常了,还报错,奇怪

试一试 
Query query=session.createQuery("select bull from Bulletin as bull");
------解决方案--------------------
Query query=session.createQuery("from Bulletin ");

但我只想取几个字段啊,不想全取啊

============================================

select b.id from meis.hibernate.BULLETIN b 

试试完整的类名~~~~
------解决方案--------------------
select b.id from meis.hibernate.BULLETIN b