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?我进行的每笔交易都有问题!在这种情况下,我得到“信息:非法尝试将集合与两个打开的会话相关联”。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 回答