1新建Person.java
package com.oa.model;
/**
* @author sala
* @hibernate.class table="t_person"
*/
public class Person {
/**
* @hibernate.id generator-class="native"
*/
private int id;
/**
* @hibernate.property
*/
private String name;
/**
* @hibernate.property
*/
private String sex;
/**
* @hibernate.property
*/
private String address;
/**
* @hibernate.property
*/
private String duty;
/**
* @hibernate.property
*/
private String phone;
/**
* @hibernate.many-to-one
*/
private Organization org;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDuty() {
return duty;
}
public void setDuty(String duty) {
this.duty = duty;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Organization getOrg() {
return org;
}
public void setOrg(Organization org) {
this.org = org;
}
}
2.新建
Organization.java
package com.oa.model;
import java.util.Set;
/**
*
* @author sala
* @hibernate.class table "t_organization"
*/
public class Organization {
/**
* @hibernate.id generator-class="native"
*/
private int id;
/**
* @hibernate.property
*/
private String name;
/**
* @hibernate.property
*/
private String sn;
/**
* @hibernate.property
*/
private String description;
/**
* @hibernate.many-to-one column="pid"
*/
private Organization parent;
/**
* @hibernate.set
* @hibernate.key column="pid"
* @hibernate.one-to-many class="com.oa.model.Organization"
*/
private Set<Organization> children;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Organization getParent() {
return parent;
}
public void setParent(Organization parent) {
this.parent = parent;
}
public Set<Organization> getChildren() {
return children;
}
public void setChildren(Set<Organization> children) {
this.children = children;
}
}
3.1新建源文件夹test
3.2在test文件夹下新建com.oa.model包
3.3导入junit.jar包
3.4新建一个工具包类HibernateUtils
(因为每建一个测试类或者一个测试方法都要获取SessionFactory,se,开启事物,关闭事物)
package com.oa.model;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory factory;
static{
try {
//加.configure()为了读取xml文件,默认是读取property文件
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
//定义一些方法
public static SessionFactory getSessionFactory(){
return factory;
}
public static Session getSession(){
return factory.openSession();
}
public static void closeSessionFactory(Session session){
if(session!=null){
if(session.isOpen()){
session.close();
}
}
}
}
用static定义成一个静态的(不用实例化一个对象就可以使用,这个包属于类本身,不属于某个对象)
3.5在包下新建测试类OrgTest.java
package com.oa.model;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import junit.framework.TestCase;
public class OrgTest extends TestCase {
public void testOrg1(){
Session session =null;
try {
session = HibernateUtils.getSession();
//开启事物
session.beginTransaction();
Set<Organization> set=new HashSet<Organization>();
Organization org2 = new Organization();
org2.setName("org2");
set.add(org2);
Organization org3 = new Organization();
org3.setName("org3");
set.add(org3);
Organization org4 = new Organization();
org4.setName("org4");
set.add(org4);
Organization org1 = new Organization();
org1.setName("org1");
org1.setChildren(set);
session.save(org1);
session.save(org2);
session.save(org3);
session.save(org4);
//以事务的方式提交
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行测试抛出 JDBC Driver class not found: com.mysql.jdbc.Driver 异常
原因:没有启动tomcat 手动读取xml文件
解决:在下包中添加mysql的jdbc驱动
use oa;
select * from t_organization;
存储成功
测试加载数据:
public void testLoadOrg(){
Session session =null;
try {
session = HibernateUtils.getSession();
//开启事物
session.beginTransaction();
//parent --> children
Organization org2=(Organization) session.load(Organization.class, 2);
System.out.println(org2.getName()+","+org2.getParent().getName());
//以事务的方式提交
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void testLoadOrg2(){
Session session =null;
try {
session = HibernateUtils.getSession();
//开启事物
session.beginTransaction();
//parent --> children
Organization org1=(Organization) session.load(Organization.class, 1);
Set<Organization> set = org1.getChildren();
for(Organization org : set){
System.out.println(org.getName());
}
//以事务的方式提交
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
}
当读取hibernate.cfg.xml时 Create表示重新创建,原来有的将被删除。Upatate表示更新的意思
property name="hibernate.hbm2ddl.auto">create</property>