线程" main"中的例外情况java.lang.IllegalStateException:Session / EntityManager已关闭

时间:2017-06-11 17:25:31

标签: java hibernate nhibernate-mapping

我想运行这个非常基本的Hibernate代码:

public static void main(String[] args) throws Exception
    {

        SessionFactory sessFact = HibernateUtil.getSessionFactory();
        Session session = sessFact.getCurrentSession();
        Transaction tr = session.beginTransaction();

        SystemUsers myContact = new SystemUsers(3);
        SystemUsers yourContact = new SystemUsers(4);
        SystemUsers yourContsact = new SystemUsers(5);
        SystemUsers yourContssssact = new SystemUsers(6);
        SystemUsers yourContssssasssct = new SystemUsers(7);

        // Saving to the database
        session.save(myContact);
        session.save(yourContact);
        session.save(yourContsact);
        session.save(yourContssssact);
        session.save(yourContssssasssct);

        session.flush();
        tr.commit();

        List<SystemUsers> contactList = session.createQuery("from SYSTEM_USERS").list();

        for (SystemUsers contact : contactList)
        {
            System.out.println("Id: " + contact.getId());
        }

        System.out.println("Successfully inserted");
        sessFact.close();
    }

配置文件:

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLiteDialect</property>
        <property name="hibernate.connection.driver_class">org.sqlite.JDBC</property>
        <property name="hibernate.connection.url">jdbc:sqlite:/C:/sqlite/test.sqlite</property>
        <property name="hibernate.connection.username"></property>
        <property name="hibernate.connection.password"></property>
                <property name="hibernate.connection.release_mode">auto</property>
                <property name="current_session_context_class">thread</property>
                <property name="hibernate.connection.autoReconnect">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping class="com.web.models.SystemUsers"/>
    </session-factory>
</hibernate-configuration>

我收到错误:

Exception in thread "main" java.lang.IllegalStateException: Session/EntityManager is closed
    at org.hibernate.internal.AbstractSharedSessionContract.checkOpen(AbstractSharedSessionContract.java:337)
    at org.hibernate.engine.spi.SharedSessionContractImplementor.checkOpen(SharedSessionContractImplementor.java:135)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:648)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:102)

我使用最新的Hibernate版本。你能给我一些建议我如何解决这个问题吗?

我可能需要初始化不同的工厂吗?

1 个答案:

答案 0 :(得分:0)

我认为问题在于

session.flush();//this will flush all the data in the session so for commmiting there wont be any data.
tr.commit();

试试这个。

tr.commit();
 session.flush();