我正在 Netbeans 8
中尝试 Hibernate 4
,问题是提交完成后应用程序仍在运行。
这是代码
public class TestHibernateAnotation {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Users user = new Users();
Users user2 = new Users();
user2.setUser_name("Djalil");
user.setUser_name("Daniel");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(user);
session.save(user2);
session.getTransaction().commit();
session.close();
System.out.print("End of code");
}
}
我的 hibernate 配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate- configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/SalesTest</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="testhibernateanotation.NewClass"/>
<mapping class="testhibernateanotation.Users"/>
关于这个问题的任何想法 谢谢,
最佳答案
您需要在 session.close();
之后显式销毁服务注册表。这似乎是较新的 hibernate 版本中的一个错误。所以基本上你最后清理的代码应该是这样的:
sessionFactory.close();
StandardServiceRegistryBuilder.destroy(sessionFactory.getSessionFactoryOptions().getServiceRegistry());
插件:我发布了一个示例 here - 包括这个问题。
关于Java 应用程序在运行我的代码后不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30847009/