Hibernate Query.list()没有返回任何值

时间:2016-02-09 05:03:16

标签: hibernate

Hibernate Query.list()没有返回任何值。即使它正在生成正确的查询我已经检查了sessionFactory的值,它也没有打印

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/es5</property>

<property name="hibernate.connection.password">root</property>

<property name="hibernate.connection.username">root</property>

 <property name="hibernate.connection.pool_size">10</property>

 <property name="show_sql">true</property>

 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

 <mapping resource="com/d2d/bean/hibernate.hbm.xml"/>

 </session-factory>

package com.d2d.util;

import org.hibernate.SessionFactory;   import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory 
 {
  static SessionFactory sf = null;

    static 
     {
        Configuration cfg = new Configuration();
       cfg  = cfg.configure("hibernate.cgf.xml");


        sf = cfg.buildSessionFactory();
         System.out.println(cfg);
       }

      public static SessionFactory getSessionFactory()
      {
           return sf;
        }


       }







    ****this is the class where i want to access persistance class****

package com.d2d.services;

import java.util.List;

  import org.hibernate.Criteria;
 import org.hibernate.Query;
 import org.hibernate.Session;
import org.hibernate.SessionFactory;

 import com.d2d.bean.Seller;
 import com.d2d.util.HibernateSessionFactory;
 import com.d2d.util.HibernateTemplate;

  public class SellerManager 
   {

      static Seller seller;
    public static Seller getSellerFromId(String id)
    {
       seller = (Seller) HibernateTemplate.getObject(Seller.class, id);
         return       seller;
      }

    public static Seller  getSellerByEmail(String email)
    {

           SessionFactory sf =                     HibernateSessionFactory.getSessionFactory();
         Session session = sf.openSession();



          Query q =session.createQuery("FROM Seller s where s.email ='"+email+"'");

         System.out.println(sf);
          //-----this is printing nothing-----------
         List list = q.list();

            boolean status = false;
          Seller seller2 = null;
         for(int i=0;i<list.size();i++)
         {
            Seller seller1 = (Seller)list.get(i);
            if(email.equals(seller1.getEmail()))
            {
               status = true;
               seller2 = seller1;                
            }


       }
        System.out.println("seller is"+seller2);
        return seller2;



       }

   }




      package com.d2d.util;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateTemplate 
{

    static SessionFactory sf = null ;
    public static Object  addObject(Object user)
     {
        Object id = null;

        System.out.println(user);
        try
        {
            System.out.println("add object start...");

            sf  = HibernateSessionFactory.getSessionFactory();

            Session session = sf.openSession();

            Transaction tx = session.beginTransaction();
            id = session.save(user);
            tx.commit();
            session.close();
        }

        catch(Exception ex)
        {
            System.out.println(ex);
        }
        return id;

     }  
    public static Object getObject(Class obj,Serializable id)
    {
        Object o = null; 
        try
         {
            SessionFactory sf = HibernateSessionFactory.getSessionFactory();
            Session session =  sf.openSession();

            Transaction tx = session.beginTransaction();
            o = session.load(obj, id);
            tx.commit();
            session.close();


         }
        catch(Exception ex)
        {
            System.out.println(ex);
        }
        return o;

    }

}
   please help me out................

1 个答案:

答案 0 :(得分:0)

Hi Inder尝试在查询浏览器中运行您的查询,如oracle或MySql我认为您的查询不返回任何内容.java中的Query.list()仅适用于选择查询而不适用于更新和插入。Query.list()不返回任何内容在列表中,如果您的查询没有返回任何结果集。