我知道这个问题已被问过千次,但我无法解决我的问题...我在互联网上搜索并找不到解决办法......这是错误!
Hibernate 5.1
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"/>
<property name="hibernate.default_schema">protein_tracker</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/twentyone/User.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 Jul 18, 2016 9:29:14 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.twentyone.User" table="USERS">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="goal" type="int">
<column name="GOAL" />
</property>
<property name="total" type="int">
<column name="TOTAL" />
</property>
</class>
</hibernate-mapping>
HibernateUtilities类
package com.twentyone;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtilities {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
static {
try {
Configuration configuration = new Configuration().configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (HibernateException e1) {
e1.printStackTrace();
System.out.println("Problem");
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
主要
package com.twentyone;
import org.hibernate.Session;
public class Program {
public static void main(String[] args) {
System.out.println("Hayden");
Session session = HibernateUtilities.getSessionFactory().openSession();
session.beginTransaction();
User user = new User();
user.setName("Hayden");
user.setGoal(250);
session.save(user);
session.getTransaction().commit();
session.close();
HibernateUtilities.getSessionFactory().close();
}
}
答案 0 :(得分:0)
这已成为一个经典问题:您无法使用Hibernate 4配置代码来配置Hibernate 5.
https://stackoverflow.com/a/32711654/3405171
只需使用:
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
以上代码有效,如果您在hibernate.properties
中有属性。你在hibernate.cfg.xml
中拥有它。我无法记住,它也可能适用于这种情况。
无论如何,您可以使用新的Hibernate 5 Bootstrap API。