Java Hibernate查询结果为空
我正在学习hibernate,并制作了一个简单的程序从我的sql中读取一个表。Java Hibernate查询结果为空,java,mysql,hibernate,Java,Mysql,Hibernate,我正在学习hibernate,并制作了一个简单的程序从我的sql中读取一个表。执行查询后得到的结果列表大小为零,即使该表中有14条记录。我显示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
执行查询后得到的结果列表大小为零,即使该表中有14条记录。
我显示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_
我还附上代码以供参考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();
实体类: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;
}
}
请帮助我,因为我在这个问题上无法归零?我也有同样的问题。我通过在创建查询之前开始一个事务并在打印数据之后提交来修复它。我不知道为什么我需要一个事务来查询数据库。我也有同样的问题。我通过在创建查询之前开始一个事务并在打印数据之后提交来修复它。我不知道为什么我需要一个事务来查询数据库。在测试中遇到了同样的问题:
@Test
public void readMessage() {
try (Session session = factory.openSession()) {
List<Message> list = session.createQuery("from Message", Message.class).list();
assertEquals(list.size() > 3, true);
}
}
@测试
public void readMessage(){
try(Session Session=factory.openSession()){
List List=session.createQuery(“来自消息”,Message.class).List();
assertEquals(list.size()>3,true);
}
}
在hibernate.cfg.xml
文件中使用:update
参考资料:在测试中遇到了相同的问题:
@Test
public void readMessage() {
try (Session session = factory.openSession()) {
List<Message> list = session.createQuery("from Message", Message.class).list();
assertEquals(list.size() > 3, true);
}
}
@测试
public void readMessage(){
try(Session Session=factory.openSession()){
List List=session.createQuery(“来自消息”,Message.class).List();
assertEquals(list.size()>3,true);
}
}
在hibernate.cfg.xml
文件中使用:update
参考:您可以发布您的实体类吗?您确定您指向的是正确的数据库吗?以确保我指向的是正确的数据库。我使用hibernate插入了一条新记录。它工作正常hibernate在插入记录时生成了什么查询?若要解决此问题,请在hibernate.cfg.xml:)中设置hibernate.hbm2ddl.auto=update。是否可以发布实体类?是否确定指向正确的数据库?是否确定我指向正确的数据库。我使用hibernate插入了一条新记录。它工作正常hibernate在插入记录时生成了什么查询?要解决这个问题,请在hibernate.cfg.xml中设置hibernate.hbm2ddl.auto=update:)谢谢,我在创建查询之前添加了
session.beginTransaction().commit()
,解决了问题。我不想问为什么,我倾向于在未来的项目中不再使用hibernate谢谢,我在创建查询之前添加了session.beginTransaction().commit()
,这就解决了问题。我不会问为什么,我倾向于在未来的项目中不再使用hibernate