简体   繁体   English

Hibernate 查询结果为空

[英]Hibernate query result is empty

I am learning hibernate and made a simple program to read a table from my sql.我正在学习休眠并制作了一个简单的程序来从我的 sql 中读取一个表。
The result list that I am getting after executing the query is having size zero even though table has 14 records in it.即使表中有 14 条记录,我在执行查询后得到的结果列表的大小为零。
I made the show_sql to see the sql logs and I am getting this :我制作了 show_sql 来查看 sql 日志,我得到了这个:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select insurance0_.lngInsuranceId as col_0_0_ from insurance insurance0_

I am also attaching code for reference我也附上代码以供参考

Session session = new Configuration().configure("Hibernate.cfg.xml").buildSessionFactory().openSession();

    String SQL_QUERY ="from Insurance insurance";
    Query query = session.createQuery(SQL_QUERY);
    System.out.println("size of list is: "+query.list().size());
    List resultList = query.list();

    for (Iterator iterator = resultList.iterator(); iterator.hasNext();) {
        Insurance object = (Insurance) iterator.next();
        System.out.println("insurance name is: "+object.getInsuranceName());
        System.out.println("insurance amount is: "+object.getInvestementAmount());

    }
    session.close();

Entity class :实体类:

public class Insurance {

  private long lngInsuranceId;
  private String insuranceName;
  private int investementAmount;
  private Date investementDate;
public long getLngInsuranceId() {
    return lngInsuranceId;
}
public void setLngInsuranceId(long lngInsuranceId) {
    this.lngInsuranceId = lngInsuranceId;
}
public String getInsuranceName() {
    return insuranceName;
}
public void setInsuranceName(String insuranceName) {
    this.insuranceName = insuranceName;
}
public int getInvestementAmount() {
    return investementAmount;
}
public void setInvestementAmount(int investementAmount) {
    this.investementAmount = investementAmount;
}
public Date getInvestementDate() {
    return investementDate;
}
public void setInvestementDate(Date investementDate) {
    this.investementDate = investementDate;
}

} }

Please help as I am not able to zero in this issue?请帮忙,因为我无法在这个问题上归零?

I had the same problem.我有同样的问题。 I fixed it by beginning a transaction before creating query and committing after printing data.我通过在创建查询之前开始事务并在打印数据之后提交来修复它。 I don't know why I need to have a transaction to query the database.我不知道为什么我需要有一个事务来查询数据库。

Got the same problem with test:测试遇到同样的问题:

@Test
public void readMessage() {
    try (Session session = factory.openSession()) {
        List<Message> list = session.createQuery("from Message", Message.class).list();
        assertEquals(list.size() > 3, true);
    }
}

use: <property name="hbm2ddl.auto">update</property> in your hibernate.cfg.xml file.使用: <property name="hbm2ddl.auto">update</property>在您的hibernate.cfg.xml文件中。

Reference: What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do参考: Hibernate hbm2ddl.auto 配置的可能值是什么,它们有什么作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM