Java 如果我的表中有数据,为什么 Hibernate 条件返回空列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21526867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why Hibernate criteria return empty list if I have data in my table?
提问by Ankit Lamba
I didn't understand why I am getting empty list from criteria
, and I have data in my table.
我不明白为什么我从 获取空列表criteria
,我的表中有数据。
code where I'm getting List:
我得到列表的代码:
hibernateSession_destination = HibernateUtilReports.INSTANCE.getSession();
Criteria criteria = hibernateSession_destination.createCriteria(nr_rec_backup_rule.class);
List list = criteria.list();
System.out.println("List length ======= " + list.size()); // prints size = 0
My HibernateUtilReports.java :
我的 HibernateUtilReports.java :
public enum HibernateUtilReports {
INSTANCE;
public static SessionFactory sessionFactory = null;
private synchronized SessionFactory getSessionFactory(){
if(sessionFactory == null){
Configuration config = new Configuration();
config.addAnnotatedClass(contaque_recording_log.class);
config.addAnnotatedClass(contaque_servers.class);
config.configure("reportshibernate.cfg.xml"); // is here any error???
Properties configProperties = config.getProperties();
ServiceRegistryBuilder serviceRegisteryBuilder = new ServiceRegistryBuilder();
ServiceRegistry serviceRegistry = serviceRegisteryBuilder.applySettings(configProperties).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
}
public Session getSession(){
return getSessionFactory().openSession();
}
}
My reportshibernate.cfg.xml:
我的报告shibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.url">jdbc:oracle:thin:@8080/xe</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">1234</property>
<property name="c3p0.minPoolSize">2</property>
<property name="c3p0.maxPoolSize">100</property>
<property name="c3p0.timeout">1000</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.zeroDateTimeBehavior">convertToNull</property>
</session-factory>
Where am I going wrong???
我哪里错了???
Edited:
编辑:
Note: Since "show_sql" is set to 'true' in my xml, but I am not getting any SQL query on my console.
注意:由于“show_sql”在我的 xml 中设置为“true”,但我在控制台上没有收到任何 SQL 查询。
采纳答案by Mathews
For those who are still looking for an answer to this question as I was, Please check your hibernate config xml and make sure your hibernate entity is declared there
对于那些像我一样仍在寻找这个问题答案的人,请检查您的 hibernate config xml 并确保您的 hibernate 实体在那里声明
回答by Bhaskar
While working with Criteria API, we need to provide the expression or alias which basically sets the Criteria which is ought to be implemented in the calling method/class.
在使用 Criteria API 时,我们需要提供表达式或别名,它基本上设置了应该在调用方法/类中实现的 Criteria。
Consider, that I want to SET a criteria for an ORDER bean which has orderId, orderName, orderType as its input attributes on the basis of which I will set the criteria or fetch the data. My approach will be something like this:
考虑一下,我想为一个 ORDER bean 设置一个条件,该 bean 具有 orderId、orderName、orderType 作为其输入属性,我将在此基础上设置条件或获取数据。我的方法是这样的:
hibernateSession_destination = HibernateUtilReports.INSTANCE.getSession();
Criteria criteria = hibernateSession_destination.createCriteria(Order.class);
criteria.add(Expression.eq("orderId", orderId)); //1
criteria.add(Expression.like("orderType", siteType)); //2
criteria.add(Expression.in("orderId", siteId)); //3
List list = criteria.list();
System.out.println("List length ======= " + list.size());
Note: It's not necessary for me to include all of 1,2,3 marked above. But for setting Order Criteria. You can use this as a work-around if you are not able to fetch the entire schema due to any restrictions.
注意:我没有必要包括上面标记的所有 1、2、3。但用于设置订单标准。如果由于任何限制而无法获取整个架构,您可以将其用作解决方法。
However, you may also need to check your db connectivity. This may be an issue which isn't allowing your application to fetch the data.
但是,您可能还需要检查数据库连接。这可能是一个不允许您的应用程序获取数据的问题。