以下是基于一个人对应一个号码的映射配置及级联CURD例子
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.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.username">root</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="club/lygangdai/entity/Person.hbm.xml"/>
<mapping resource="club/lygangdai/entity/Card.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Person.java
package club.lygangdai.entity;
/**
* @ClassName: Province
* @Description: TODO(人 类)
* @author Uncle
* @date 2018年8月7日 上午9:50:48
*
*/
public class Person {
private int pid; //id
private String pname; //人名
private Card c; //卡对象
/**
* @Description: TODO(这里用一句话描述这个类的作用)
* @param pid
* @param pname
*/
public Person(int pid, String pname) {
super();
this.pid = pid;
this.pname = pname;
}
/**
* @Description: TODO(这里用一句话描述这个类的作用)
*/
public Person() {
super();
}
/**
* @return pid
*/
public int getPid() {
return pid;
}
/**
* @param pid 要设置的 pid
*/
public void setPid(int pid) {
this.pid = pid;
}
/**
* @return pname
*/
public String getPname() {
return pname;
}
/**
* @param pname 要设置的 pname
*/
public void setPname(String pname) {
this.pname = pname;
}
/**
* @return c
*/
public Card getC() {
return c;
}
/**
* @param c 要设置的 c
*/
public void setC(Card c) {
this.c = c;
}
/* (非 Javadoc)
* @Title: toString
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Province [pid=" + pid + ", pname=" + pname + "]";
}
}
Person.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 2018??8??7?? ????10:44:13 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<!--映射实体类对象,生成默认为person的数据库表-->
<class name="club.lygangdai.entity.Person" table="person">
<id name="pid" type="int"> <!-- 指定类的id -->
<column name="pid" /> <!-- 对应表的列 -->
<generator class="native" /><!-- 自增 -->
</id>
<property name="pname" type="java.lang.String">
<column name="pname" />
</property>
<one-to-one name="c" class="club.lygangdai.entity.Card" cascade="all-delete-orphan"></one-to-one> <!-- 映射 一对一关系 -->
</class>
</hibernate-mapping>
在 cascade(级联)的配置
Card.java
package club.lygangdai.entity;
/**
* @ClassName: city
* @Description: TODO(卡类)
* @author Uncle
* @date 2018年8月7日 上午9:40:17
*
*/
public class Card {
private int cid; //id
private String cname; //卡号
private Person p; //person对象
/**
* @return p
*/
public Person getP() {
return p;
}
/**
* @param p 要设置的 p
*/
public void setP(Person p) {
this.p = p;
}
/**
* @Description: TODO(这里用一句话描述这个类的作用)
*/
public Card() {
super();
}
/**
* @return cid
*/
public int getCid() {
return cid;
}
/**
* @param cid 要设置的 cid
*/
public void setCid(int cid) {
this.cid = cid;
}
/**
* @return cname
*/
public String getCname() {
return cname;
}
/**
* @param cname 要设置的 cname
*/
public void setCname(String cname) {
this.cname = cname;
}
/* (非 Javadoc)
* @Title: toString
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Card [cid=" + cid + ", cname=" + cname + "]";
}
}
Card.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 2018??8??7?? ????10:44:13 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<!--映射实体类对象,生成默认为card的数据库表-->
<class name="club.lygangdai.entity.Card" table="card">
<id name="cid" type="int"> <!-- 对应实体类的id -->
<column name="cid" /> <!-- 对应表的列 -->
<!-- 采用foreign生成策略,forgeign会取得另外一个关联对象的标识 -->
<generator class="foreign"><!-- 外键 -->
<param name="property">p</param><!-- 指定person表,对应的id -->
</generator>
</id>
<property name="cname" type="java.lang.String">
<column name="cname" />
</property>
<!-- 映射 一对一关系 -->
<one-to-one name="p" class="club.lygangdai.entity.Person"></one-to-one>
</class>
</hibernate-mapping>
HibernateTest.java
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import club.lygangdai.entity.Card;
import club.lygangdai.entity.Person;
/**
* @ClassName: HibernateTest
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Uncle
* @date 2018年8月7日 上午8:58:50
*
*/
public class HibernateTest {
@Test
public void hibernateTest() {
//读取配置文件
Configuration configuration = new Configuration().configure();
//创建工厂
SessionFactory sessionFactory = configuration.buildSessionFactory();
//session
Session session = sessionFactory.openSession();
//事务
Transaction transaction = session.beginTransaction();
//级联关系
//增加 person表和card表
// Person p = new Person();
// p.setPname("张三");
// Card c = new Card();
// c.setCname("1123456");
//互设
// p.setC(c);
// c.setP(p);
//保存
// session.save(p);
//修改
//得到person 修改card的cname
// Person p = session.get(Person.class, 2);
// p.getC().setCname("卡号");
// session.update(p);
//得到card 修改person的pname
// Card c = session.get(Card.class, 2);
// c.getP().setPname("xx");
// session.save(c);
//删除
//级联删除 先查出主表数据,再删除主表数据,子表数据同步删除(查子表数据级联删除不生效)
// Person p = session.get(Person.class, 8);
// session.delete(p);
//单独删一张表 直接新建对象,给id赋值,删除对应id的数据
// Card c = new Card();
// c.setCid(7);
// session.delete(c);
//提交
transaction.commit();
//关闭session及sessionFactory
session.close();
sessionFactory.close();
}
}
评论 (0)