2016-02-09 6 views
0

Hibernate Query.list() не возвращает значение. даже генерирует надлежащего запроса я проверил значение SessionFactory также не будет также печать НИЧТОHibernate Query.list() не возвращает значение

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

пакет com.d2d.util ;

импорт 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

Отложите свой код класса и файл hibernate.cfg –

+0

Да. Нам нужны постоянные классы и код с вашим запросом. –

+0

Основываясь на информации, предоставленной в вопросе, единственно возможная догадка заключается в следующем: в вашей базе данных нет данных – yugo

ответ

0

Hi Inder попробуйте выполнить запрос в браузере запросов, например, oracle или MySql. Я думаю, что ваш запрос ничего не возвращает. Query.list() в java работает только для избранных запросов, а не для обновлений и вставок. Query.list() ничего не возвращает в списке, если ваш запрос не возвратил никакого набора результатов.

+0

сэр, я пытаюсь найти запрос, и он ничего не возвращает, но предопределенные функции в спящем режиме -session, как session.save() работает правильно. Большое вам спасибо за ваше внимание –

Смежные вопросы