gpt4 book ai didi

java - hibernate 5 : sessionFactory is null

转载 作者:行者123 更新时间:2023-11-30 10:52:46 25 4
gpt4 key购买 nike

我在这一行得到了一个 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 相关文件:

<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">manolete</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/employee</property>
<property name="hibernate.connection.username">root</property>
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="src/ch/makery/model/Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 14-dic-2015 21:00:04 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="ch.makery.model.Employee" table="EMPLOYEE">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="firstName" type="java.lang.String">
<column name="FIRSTNAME" />
</property>
<property name="lastName" type="java.lang.String">
<column name="LASTNAME" />
</property>
</class>
</hibernate-mapping>
package ch.makery.model;

public class Employee {
private int id;
private String firstName,lastName;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}

}

我没有使用 Spring,因为我只是创建一个桌面应用程序。

最佳答案

buildSessionFactory 方法在 hibernate 4 版本中被弃用,并被新的 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());

关于java - hibernate 5 : sessionFactory is null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34289655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com