Java Hibernate 5:sessionFactory为空
我在这一行得到了Java Hibernate 5:sessionFactory为空,java,hibernate,sessionfactory,hibernate-5.x,Java,Hibernate,Sessionfactory,Hibernate 5.x,我在这一行得到了sessionFactory变量的空值: sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); 这是全班同学: import javax.imageio.spi.ServiceRegistry; import javax.transaction.Transaction; import org.hibernate.Session; import org.hi
sessionFactory
变量的空值:
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
这是全班同学:
import javax.imageio.spi.ServiceRegistry;
import javax.transaction.Transaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import ch.makery.model.Employee;
public class HelloWorld {
protected void setUp() throws Exception {
}
public static void main(String[] args) {
SessionFactory sessionFactory = null;
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
try {
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
// so destroy it manually.
StandardServiceRegistryBuilder.destroy( registry );
}
Session session = sessionFactory.openSession();
//employee = new Employee();
session.beginTransaction();
session.save(new Employee());
session.getTransaction().commit();
session.close();
}
}
这是我的Hibernate相关文件:
com.mysql.jdbc.Driver
马诺莱特
jdbc:mysql://localhost:3306/employee
根
org.hibernate.cache.internal.NoCacheProvider
创造
包ch.makery.model;
公共类雇员{
私有int-id;
私有字符串firstName,lastName;
public int getId(){
返回id;
}
public void setId(int id){
this.id=id;
}
公共字符串getFirstName(){
返回名字;
}
public void setFirstName(字符串firstName){
this.firstName=firstName;
}
公共字符串getLastName(){
返回姓氏;
}
public void setLastName(字符串lastName){
this.lastName=lastName;
}
}
我没有使用Spring,因为我只是创建了一个桌面应用程序。hibernate 4发行版中的
buildSessionFactory
方法已被弃用,并被新的API取代。如果您使用的是hibernate 4.3.0及更高版本,请尝试编写如下配置:
Configuration configuration = new Configuration().configure();
configuration.configure("your_path_hibernate_.cfg.xml");
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(ssrb.build());
当我将Hibernate代码从v4.1.8更新到v5.0.6时,我的
SessionFactory
实例为空时也遇到了问题。通过打印堆栈跟踪,我能够确定需要在构建路径中包含可选的c3p0
jar
我的第一个建议是直接从catch块打印堆栈跟踪。跟踪将为您解决问题提供一个良好的起点。因此,您的catch
块如下所示:
catch (Exception e) {
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
// so destroy it manually.
StandardServiceRegistryBuilder.destroy( registry );
e.printStackTrace();
}
但是,我注意到在配置文件中没有定义hibernate.dialogue
属性。虽然此属性不是强制性的,但说明可能有“某些原因”hibernate无法确定您使用的是哪种方言。我的第二个建议是使用hibernate.dialent
设置直接在配置文件中定义数据库方言
由于原始配置文件post表明您正在使用MySQL数据库方言,请尝试将此方言属性节点添加到会话工厂节点:
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
org.hibernate.dialogue.mysqldialogue
如果您使用的是MySQL 5.x,那么使用
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
org.hibernate.dialogue.mysql5dialogue
确保可选的c3p0
jar位于构建路径中
祝你好运 但是快速入门说,
buildSessionFactory()
它并没有被弃用:@ziiweb看看这个欢迎堆栈溢出的网站!虽然这个代码片段可以解决这个问题,但它没有解释为什么或者如何回答这个问题。请,因为这确实有助于提高你的文章质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。标记者/审阅者:
private static SessionFactory sessionFactory = createSessionFactory();
private static SessionFactory createSessionFactory() {
if (sessionFactory == null) {
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder() .configure("hibernate.cfg.xml").build();
Metadata metaData = new MetadataSources(standardRegistry).getMetadataBuilder().build();
sessionFactory = metaData.getSessionFactoryBuilder().build();
}
return sessionFactory;
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
sessionFactory.getCurrentSession().close();
}