Question

I didn't understand why I am getting empty list from criteria, and I have data in my table.

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 :

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:

<?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.

Was it helpful?

Solution

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

OTHER TIPS

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.

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:

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.

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.

Please use this :

List<Class_name> list = criteria.list();

instead of directly using:

List list = criteria.list();

We should always mention the class for which we are fetching the list.For example,if i want the data for my Student class from the database using Criteria api then,i would use somthing like this:

SessionFactory factory= new Configuration().configure().buildSessionFactory(); 
Session session =factory.openSession(); 
Criteria criteria=session.createCriteria(Student.class);
List<Student> list=criteria.list();
 
   for(Student student:list)
    {
        System.out.println("Criteria Demo:"+student.getName()); 
    
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top