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

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

 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 に問題があります。この場合、「情報: コレクションを 2 つの開いているセッションに関連付けようとする不正な試み」が表示されます。私change session.save(m); to session.merge(m);が同じページにいる限り、それは機能します。ページを変更してから戻ると、多くの例外が発生します。

4

1 に答える 1

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();

      }
}

于 2013-01-23T16:47:41.523 に答える