您的位置:首页 > 编程语言 > Java开发

(自己使用Hibernate出现的版本问题)Eclipse上使用Hibernate5.0及以上版本编写!

2017-01-24 19:59 423 查看
文章转载地址:http://bbs.csdn.net/topics/391955731

这个贴子是为了帮助初学者解决使用Eclipse并使用Hibernate5.0及以上版本(我用的是 hibernate-release-5.1.0-Final)创建第一个Hibernate HelloWorld程序时,junit4测试时出现各种问题。

未知实体类(- org.hibernate.MappingException: Unknown entity)
找不到cfg.xml包(org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource)

的解决办法。

 我的环境:

Eclipse Java EE IDE for Web Developers.Version: Mars.2 Release (4.5.2) 

jdk1.8.0_92

hibernate-release-5.1.0-Final

JBoss Tools 4.3.1 Final

1、当爆出:未知实体类(- org.hibernate.MappingException: Unknown entity)错误时,原因是:

Hibernate5.0及以上版本,与4.x以往版本不同的,最关键的代码——获得SessionFactory的方式不同


private SessionFactory sessionFactory;

private Session session;

private Transaction transaction;

// 5.0及以上版本——不用通过ServiceRegistry

Configuration configuration = new Configuration().configure();
sessionFactory = new Configuration().configure().buildSessionFactory();

session = sessionFactory.getCurrentSession();

transaction = session.beginTransaction();

// 4.x版本——需要通过ServiceRegistry

Configuration configuration = new Configuration().configure();

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = config.buildSessionFactory(serviceRegistry);

session = sessionFactory.getCurrentSession();

transaction = session.beginTransaction();

还有一个找不到类的原因: 在cfg.xml配置文件中<mapping class="" />映射没有配置正确

(1)、可以通过持久化类来配置







(2)、 可以通过.hbm.xml来配置





(3)、当然你也可以两个都加上去



------------------------------------------------------------------------------------------————----------------------------

2、当爆出:找不到cfg.xml包(org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource)错误时,原因是: cfg.xml文件放置的位置不对
你需要把.cfg.xml文件放在src中的一个Source Floder下如下图



原因在于,sessionFactory = new Configuration().configure().buildSessionFactory(); 这一段代码中获取cfg.xml文件的默认位置就是在src目录下。.

(configure()中是可以填写一个URL参数的,但是我不会写,还有,我把cfg.xml文件直接放在src目录下,有的时候可以正常运行,有的时候又说找不到cfg.xml文件。希望高手来解决这个问题,感谢!。)

贴出我的HelloWorld程序所有代码

Students

package config;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity(name="Students")
public class Students implements Serializable{
private static final long serialVersionUID = 1L;
@Id
private int sid; // 学号
private String sname; // 姓名
private String gender; // 性别
private Date brithday; // 出生日期
private String address; // 住址

public Students(){}

public Students(int sid, String sname, String gender, Date brithday, String address) {
super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.brithday = brithday;
this.address = address;
}

public int getSid() {
return sid;
}

public void setSid(int sid) {
this.sid = sid;
}

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public Date getBrithday() {
return brithday;
}

public void setBrithday(Date brithday) {
this.brithday = brithday;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", brithday=" + brithday
+ ", address=" + address + "]";
};

}

TestStudents
public class TestStudents {

private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;

@Before
public void init() {

// 获得 session工厂
sessionFactory = new Configuration().configure().buildSessionFactory();
// 获得 session
session = sessionFactory.getCurrentSession();
// 开启事务
transaction = session.beginTransaction();

}

@After
public void destory() {
transaction.commit(); // 提交事务

}

@Test
public void testSaveStudents() {
// 生成学生对象
Students students = new Students(2, "黄蓉", "女", new Date(), "桃花岛");
System.out.println(students.getSname() + students.getGender() + students.getAddress());
session.save(students); // 保存对象进入数据库
}
}
hibernate.cfg.xml
<?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.password">xxxxxx</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>

<mapping class="config.Students"/>
<mapping resource="config/Students.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Students.hbm.xml
<?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 May 22, 2016 11:40:35 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="config.Students" table="STUDENTS">
<id name="sid" type="int">
<column name="SID" />
<generator class="assigned" />
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" />
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER" />
</property>
<property name="brithday" type="java.util.Date">
<column name="BRITHDAY" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
</class>
</hibernate-mapping>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐