2013-01-23 229 views
0
<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/PP</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password"></property> 
    <property name="hibernate.show_sql">true</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property> 
    </session-factory> 
</hibernate-configuration> 

HibernateSession.java如何正確配置Hibernate

public class HibernateSession { 

    private static final SessionFactory sessionFactory; 

    static { 
     try { 

      sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
     } catch (Throwable ex) { 
      // Log the exception. 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() 
     { 
     return sessionFactory; 
     } 
} 

DataFetcher.java用於執行quesries

public class DataFetcher { 

     Session session; 

     public DataFetcher() 
      { 
      session = HibernateSession.getSessionFactory().getCurrentSession(); 
      } 

     public void Save(Marki m) 
      { 
      org.hibernate.Transaction tx = session.beginTransaction(); 
      session.save(m); 
      session.getTransaction().commit(); 

      } 
    } 

ManagedBean(name="dodaj") 
@ViewScoped 
public class DodajOferte implements Serializable { 

    private Marki marka; 

    public DodajOferte() 
     { 

     marka = new Marki(); 
     } 

    public String Dodaj() 
     { 

     DataFetcher f = new DataFetcher(); 

     try { 
      f.Save(marka); 
     } catch (HibernateException ex) { 
      System.out.println(ex.getMessage().toString()); 

      FacesMessage message = new FacesMessage("err"); 
      FacesContext context = FacesContext.getCurrentInstance(); 
      context.addMessage(null, message); 
      return null; 
     } 
     return null; 

     } 

    public Marki getMarka() 
     { 
     return marka; 
     } 

    public void setMarka(Marki marka) 
     { 
     this.marka = marka; 
     } 
} 

如何正確配置Hibernate?我每次交易都會對almogs產生問題! 在這種情況下,我收到「信息:非法嘗試將收集與兩個打開的會話相關聯」。當我change session.save(m); to session.merge(m);它工作,只要我在同一頁上。當我改變頁面,然後返回時,我得到了很多例外。

+0

看起來像merge不再工作。 – user1997553

回答

0

不要使用DataFetcher存儲會話對象。這不是一個好主意。每個會話都綁定到一個綁定它的線程的事務。我不確定這段代碼是否在容器中運行,但如果是這樣的話,hibernate會嘗試使用範圍內的事務管理器,並且你的代碼會令人困惑。將您的代碼更改爲以下內容,看看是否會改變任何內容。

public class DataFetcher {

public void Save(Marki m) 
     { 
     Session session = HibernateSession.getSessionFactory().getCurrentSession(); 
     org.hibernate.Transaction tx = session.beginTransaction(); 
     session.save(m); 
     session.getTransaction().commit(); 

     } 
}