我查看了相关问题,但仍未找到解决问题的答案。
这是我的项目结构:
- src
- java
- HibernateDemo
- HibernateTestApplication.java
- HibernateUtil.java
- Models
- Users.java
- res
- hibernate.cfg.xml
- Users.hbm.xml
这是我的hibernate.cfg.xml:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">***</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!--<property name="current_session_context_class">thread</property>-->
<!-- Disable the second-level cache -->
<!--<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>-->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--<property name="hbm2ddl.auto">update</property>-->
<mapping resource="Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这是我的Users.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Models.Users" table="users">
<meta attribute="class-description">
This class contains the users detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
这是我的Users.java:
package Models;
public class Users {
private int id;
private String name;
private int age;
public Users(){}
public Users(String name, int age){
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
这是我的HibernateUtil.java:
package HibernateDemo;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import java.io.File;
public class HibernateUtil {
private static HibernateUtil instance = null;
private static SessionFactory sessionFactory;
private static StandardServiceRegistry serviceRegistry;
private HibernateUtil(){
String hibernatePropsFilePath = "E:\\java workspace\\study\\hibernate\\src\\main\\java\\res\\hibernate.cfg.xml";
File hibernatePropsFile = new File(hibernatePropsFilePath);
Configuration configuration = new Configuration();
configuration.configure(hibernatePropsFile);
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
public static HibernateUtil getInstance(){
if(instance == null){
instance = new HibernateUtil();
}
return instance;
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
这是我的HibernateTestApplication.java:
package HibernateDemo;
import Models.Users;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class HibernateTestApplication {
public static void main(String[] args) {
HibernateTestApplication hibernateTestApplication = new HibernateTestApplication();
int userId = hibernateTestApplication.addUser("Tom", 21);
System.out.println(userId);
}
public int addUser(String name, int age){
HibernateUtil hibernateUtil = HibernateUtil.getInstance();
SessionFactory sessionFactory = hibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = null;
int userId = 0;
try{
tx = session.beginTransaction();
Users user = new Users(name, age);
userId = (Integer) session.save(user);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
return userId;
}
}
我希望你能耐心地阅读上面的代码,并找到我的代码中的真正问题,thx!
答案 0 :(得分:1)
我没有在hibernate.cfg.xml
。
Hibernate配置文件必须定义实体类:
<mapping class="Models.Users"/>
答案 1 :(得分:0)
当您提供映射资源时,请以此格式提供绝对路径:
<mapping resource="res/Users.hbm.xml"
/&GT;
如果是通过类名映射,(当它将类解析为包名时)
<mapping class="Models.Users"
/&GT;
如上所述不起作用,请尝试构建会话工厂:
protected static SessionFactory buildSessionFactory() {
try {
SessionFactory sessionFactory = new Configuration()
.configure("/res/hibernate.cfg.xml")
.addResource("res/Users.hbm.xml")
.buildSessionFactory();
return sessionFactory;
} catch (Throwable ex) {
System.err.println("SessionFactory creation failed" + ex);
}
}
查看此加载是否正确。这使用当前类路径来解析,而不是从文件系统加载hbm文件。