简体   繁体   English

为什么我无法从表中获取数据?

[英]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.

相关问题 我在查询中使用BigDecimal时无法从mongoDB提取数据 - I am unable to Fetch data from mongoDB while whn i using BigDecimal inside query 我在尝试通过 URL 直接从表中获取数据时遇到 SQLGrammarException - I am getting a SQLGrammarException while trying to fetch data from table directly through URL 我无法从 Textfield 读取数据 - I am unable to read the data from Textfield 为什么在翻新调用之外无法从列表中获取数据,该列表存储了从API提取的数据? - Why am I not able to fetch data from the list, which stores data being fetched from an API, outside of the retrofit call? 为什么在将数据从文件复制到数组FileReader时无法获得输出 - Why I am unable to get output while copying data from file to array FileReader 无法使用Java中的JDBC从mdb文件的表中获取数据 - Unable to fetch data from table of mdb file using JDBC in Java 如何解决jdbc连接错误? 为什么我无法从 mysql 服务器获取表数据? - How to resolve the jdbc connection error ? Why I can't fetch the table data from the mysql server? 无法从数据库中获取数据 - Unable to fetch data from database 为什么我无法从另一台计算机连接到Openfire服务器? - Why am I unable to connect to openfire server from another computer? 我在 Android Studio 中编码,我需要从特定网页中获取并显示特定行的数据 - I am coding in Android Studio, and I need to fetch and display a specific line of data from a specific webpage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM