[英]Why I am unable to fetch data from the table?
There is table named leagues
, created as : 有一个名为leagues
表,创建为:
CREATE TABLE leagues(id INT NOT NULL AUTO_INCREMENT,lname VARCHAR(255),
season VARCHAR(255),PRIMARY KEY (lname),KEY id(id));
For which a mapping hibernate xml
is : 对于其映射的hibernate xml
是:
<hibernate-mapping>
<class name="pojo.Leagues" table="leagues">
<id column='lname' name='leaguename' type="string"> </id>
<property column="id" name="id" type="integer" />
<property column="season" name="season" type="string" />
</class>
</hibernate-mapping>
I am trying to get data from the leagues
table, but I am unable to. 我正在尝试从“ leagues
表中获取数据,但无法。 Here is the method that is meant to do it: 这是用于执行此操作的方法:
Query returns nothing and code never enters the loop. 查询不返回任何内容,并且代码从不进入循环。
public LinkedList<String> getLeagues() {
String hql = "select leaguename from pojo.Leagues";
lnames = new LinkedList<String>();
Configuration configuration = new Configuration().configure();
SessionFactory sessFact = configuration.buildSessionFactory();
Session sess = sessFact.openSession();
Query query = sess.createQuery(hql);
List list = query.list();
System.out.println("Heyyyyyyy:ll");
Iterator i = list.iterator();
while(i.hasNext()) {
System.out.println("Inside while block");
Leagues lname = (Leagues)i.next();
lnames.add(lname.getLeaguename());
}
return lnames;
}
Where am I making a mistake? 我在哪里出错? Why I am unable to fetch data? 为什么我无法获取数据?
The reason why you didn't get any data from the DB is you are using pure SQL query & calling 您没有从数据库获取任何数据的原因是您使用的是纯SQL查询和调用
Query query = sess.createQuery(hql);
which works on HQL as per createQuery()
- Hibernate Doc . 它根据createQuery()
-Hibernate Doc在HQL上工作。
If you want to use pure SQL query then you must use createSQLQuery()
- Hibernate Doc 如果要使用纯SQL查询,则必须使用createSQLQuery()
-Hibernate Doc
String sqlQuery = "select leaguename from pojo.Leagues";
SQLQuery query = sess.createSQLQuery(sqlQuery);
or change your query in HQL format just like below 或以HQL格式更改查询,如下所示
String hqlQuery = "from pojo.Leagues";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.