获取异常java.lang.IllegalStateException:尝试保存次要距离时关闭EntityManagerFactory

时间:2019-05-28 16:56:34

标签: java mysql hibernate

我正在学习冬眠。一切在开始时就很清楚了,但是当我尝试运行我的应用程序时,在主界面上我有两个与会话相关的操作,我得到了异常:

java.lang.IllegalStateException: EntityManagerFactory is closed

下面您可以找到我的DBManager类,在该类中有用于向DB添加实例的方法:

import org.hibernate.Session;

public class DBManager
{
    public void addAutor(String name, String lastName, Book book)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Autor autor = new Autor(name,lastName,book);
        session.save(autor);
        session.getTransaction().commit();
        HibernateUtil.getSessionFactory().close();
    }
    public Book addBook(int ISBN, String name, String autorName)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Book book = new Book(ISBN,name,autorName);
        session.save(book);
        session.getTransaction().commit();
        HibernateUtil.getSessionFactory().close();
        return book;
    }
}

这是我的HibernateUtil类,用于配置会话:

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            Configuration configuration = new Configuration();
            configuration.addAnnotatedClass(Autor.class);
            configuration.addAnnotatedClass(Book.class);
            configuration.configure();

            StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
            standardServiceRegistryBuilder.applySettings(configuration.getProperties());
            ServiceRegistry serviceRegistry = standardServiceRegistryBuilder.build();

            return configuration.buildSessionFactory(serviceRegistry);
        }
        catch(Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }

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

最后是主类:

public class Test
{

    public static void main (String args[])
    {
        DBManager dbManager = new DBManager();
        Book book =  dbManager.addBook(1,"Hobbit","Tolkien");
        dbManager.addAutor("JRR","Tolkien",book);
    }
}

如您所见,我只是放书,然后尝试放Autor。 secound方法可能导致异常的原因是什么?我想我有做。结束会话和打开会话是错误的,但是我无法弄清楚什么地方出了错。

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案:

我为整个类DBManager创建了一个sessionFactory对象:

private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

因此,在每种方法中,我都打开会话,然后最后关闭,例如:

    public void addAutor(String name, String lastName, Book book)
    {
        Session session=sessionFactory.openSession();
        session.beginTransaction();
        Autor autor = new Autor(name,lastName,book);
        session.save(autor);
        session.getTransaction().commit();
        session.close();
    }

也许有人会遇到同样的问题。