这一对一,一对多,多对多的表关系
创建一个maven的项目
pro.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"User实体类
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>G160628_hibernate02</groupId>
<artifactId>G160628_hibernate02</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>G160628_hibernate02 Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<argLine>-Dfile.encoding=UTF-8</argLine>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b07</version>
<scope>provided</scope>
</dependency>
<!-- 加入hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- Mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
</dependencies>
<build>
<finalName>G160628_hibernate02</finalName>
</build>
</project>
package com.zking.entity;这是User.hbm.xml的映射
public class User {
private int uid;
private String uname;
private Card card;
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String uname) {
super();
this.uname = uname;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
}
<?xml version="1.0"?>这是Card的实体类
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-8-22 14:32:20 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity.User" table="USER">
<id name="uid" type="int">
<column name="UID" />
<generator class="native" />
</id>
<property name="uname" type="java.lang.String">
<column name="UNAME" />
</property>
<one-to-one name="card" class="com.zking.entity.Card" cascade="all-delete-orphan"></one-to-one>
</class>
</hibernate-mapping>
package com.zking.entity;这是Card.hbm.xml
public class Card {
private int uid;
private String cnum;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Card() {
super();
}
public Card(String cnum) {
super();
this.cnum = cnum;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getCnum() {
return cnum;
}
public void setCnum(String cnum) {
this.cnum = cnum;
}
}
<?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 2017-8-22 14:32:20 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity.Card" table="CARD">
<id name="uid" type="int">
<column name="UID" />
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<property name="cnum" type="java.lang.String">
<column name="CNUM" />
</property>
<one-to-one name="user" class="com.zking.entity.User"></one-to-one>
</class>
</hibernate-mapping>
这是Province的实体类
package com.zking.entity;Province.hbm.xml
import java.util.HashSet;
import java.util.Set;
public class Province {
private int pid;
private String pname;
private Set<City> cities=new HashSet<City>();
public Set<City> getCities() {
return cities;
}
public void setCities(Set<City> cities) {
this.cities = cities;
}
public Province() {
super();
}
public Province(String pname) {
super();
this.pname = pname;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
}
<?xml version="1.0"?>City的实体类
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-8-22 15:14:11 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity.Province" table="PROVINCE">
<id name="pid" type="int">
<column name="PID" />
<generator class="native" />
</id>
<property name="pname" type="java.lang.String">
<column name="PNAME" />
</property>
<set name="cities" table="city" cascade="save-update">
<key column="pid"></key>
<one-to-many class="com.zking.entity.City"/>
</set>
</class>
</hibernate-mapping>
package com.zking.entity;
public class City {
private int cid;
private String cname;
//private int pid;
private Province province;
public Province getProvince() {
return province;
}
public void setProvince(Province province) {
this.province = province;
}
public City() {
super();
// TODO Auto-generated constructor stub
}
public City(String cname) {
super();
this.cname = cname;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
//public int getPid() {
//return pid;
//}
//public void setPid(int pid) {
//this.pid = pid;
//}
}
City.hbm.xml
<?xml version="1.0"?>Roles 的实体类
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-8-22 15:14:11 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity.City" table="CITY">
<id name="cid" type="int">
<column name="CID" />
<generator class="native" />
</id>
<property name="cname" type="java.lang.String">
<column name="CNAME" />
</property>
<!-- <property name="pid" type="int">
<column name="PID" />
</property> -->
<many-to-one name="province" class="com.zking.entity.Province" column="pid"></many-to-one>
</class>
</hibernate-mapping>
package com.zking.entity;Roles.hbm.xml
import java.util.HashSet;
import java.util.Set;
public class Roles {
private int rid;
private String rname;
private Set<Users> users=new HashSet<Users>();
public Set<Users> getUsers() {
return users;
}
public void setUsers(Set<Users> users) {
this.users = users;
}
public Roles() {
super();
}
public Roles(String rname) {
super();
this.rname = rname;
}
public int getRid() {
return rid;
}
public void setRid(int rid) {
this.rid = rid;
}
public String getRname() {
return rname;
}
public void setRname(String rname) {
this.rname = rname;
}
}
<?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 2017-8-22 15:41:45 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity.Roles" table="ROLES">
<id name="rid" type="int">
<column name="RID" />
<generator class="native" />
</id>
<property name="rname" type="java.lang.String">
<column name="RNAME" />
</property>
<set name="users" table="u_r">
<key column="rid"></key>
<many-to-many class="com.zking.entity.Users" column="uid"></many-to-many>
</set>
</class>
</hibernate-mapping>
Users实体类
package com.zking.entity;
import java.util.HashSet;
import java.util.Set;
public class Users {
private int uid;
private String uname;
private Set<Roles> roles=new HashSet<Roles>();
public Set<Roles> getRoles() {
return roles;
}
public void setRoles(Set<Roles> roles) {
this.roles = roles;
}
public Users() {
super();
// TODO Auto-generated constructor stub
}
public Users(String uname) {
super();
this.uname = uname;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
}
User.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 2017-8-22 15:41:45 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.zking.entity.Users" table="USERS">
<id name="uid" type="int">
<column name="UID" />
<generator class="native" />
</id>
<property name="uname" type="java.lang.String">
<column name="UNAME" />
</property>
<set name="roles" table="u_r" cascade="save-update">
<key column="uid"></key>
<many-to-many class="com.zking.entity.Roles" column="rid"></many-to-many>
</set>
</class>
</hibernate-mapping>
配置文件
<?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">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/zking/entity/User.hbm.xml"/>
<mapping resource="com/zking/entity/Card.hbm.xml"/>
<mapping resource="com/zking/entity/Province.hbm.xml"/>
<mapping resource="com/zking/entity/City.hbm.xml"/>
<mapping resource="com/zking/entity/Users.hbm.xml"/>
<mapping resource="com/zking/entity/Roles.hbm.xml"/>
</session-factory>
</hibernate-configuration>
package com.zking.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.zking.entity.Card;
import com.zking.entity.User;
public class TestOneToOne {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void before(){
Configuration configuration=new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void after(){
transaction.commit();
session.close();
sessionFactory.close();
}
public void testAdd(){
User user=new User("张三");
Card card=new Card("43110731");
//互设
user.setCard(card);
card.setUser(user);
session.save(user);
session.save(card);
}
public void testGet(){
User user=session.get(User.class, 1);
System.out.println(user.getCard().getCnum());
}
public void testDelete(){
//删除主表
//User user=session.get(User.class, 2);
//session.delete(user);
//删除从表
Card card=session.get(Card.class, 3);
card.getUser().setCard(null);
session.delete(card);
}
}
一对多测试
package com.zking.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.zking.entity.Card;
import com.zking.entity.City;
import com.zking.entity.Province;
import com.zking.entity.User;
public class TestOneToMany {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void before(){
Configuration configuration=new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void after(){
transaction.commit();
session.close();
sessionFactory.close();
}
public void testAdd(){
Province province=new Province("湖南省");
City city1=new City("长沙市");
City city2=new City("娄底市");
City city3=new City("其他市");
province.getCities().add(city1);
province.getCities().add(city2);
province.getCities().add(city3);
city1.setProvince(province);
city2.setProvince(province);
city3.setProvince(province);
session.save(province);
}
}
多对多测试
package com.zking.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.zking.entity.Card;
import com.zking.entity.City;
import com.zking.entity.Province;
import com.zking.entity.Roles;
import com.zking.entity.User;
import com.zking.entity.Users;
public class TestManyToMany {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void before(){
Configuration configuration=new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void after(){
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public void testAdd(){
Users users1=new Users("辉辉");
Users users2=new Users("静静");
Roles roles1=new Roles("普通用户");
Roles roles2=new Roles("管理员");
Roles roles3=new Roles("超级管理员");
Roles roles4=new Roles("VIP");
users1.getRoles().add(roles1);
users1.getRoles().add(roles3);
users1.getRoles().add(roles4);
users2.getRoles().add(roles2);
users2.getRoles().add(roles4);
//roles1.getUsers().add(users1);
//roles2.getUsers().add(users2);
//roles3.getUsers().add(users1);
//roles4.getUsers().add(users1);
//roles4.getUsers().add(users2);
session.save(users1);
session.save(users2);
}
}