首页 > 代码库 > Hibernate——ORMapping
Hibernate——ORMapping
ORMapping
第一种:一对一单向外键关联
一、Annotaion配置
小实验1:
(1)编写hunsband.java,属性为id,name,wife。id是主键
(2)编写wife.java,属性为id,name。id是主键
(3)实现方式:在husband.java中,用wife作为外键关联wife.java中的主键id
Husband.java
package com.zgy.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Husband {
private int id;
private String name;
private Wife wife;
@Id
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;
}
@OneToOne
@JoinColumn(name="wifeId")
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
Wife.java
package com.zgy.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Wife {
private int id;
private String name;
@Id
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;
}
}
Junit测试
package com.zgy.hibernate.model;
import static org.junit.Assert.*;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class ORMapppingTest {
public static SessionFactory sf = null;
@BeforeClass
public static void beforeClass(){
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@Test
public void test() {
Husband h = new Husband();
Wife w = new Wife();
Session session2 = sf.openSession();
session2.beginTransaction();
w.setId(1);
w.setName("李四");
session2.save(w);
session2.getTransaction().commit();
Session session1 = sf.openSession();
session1.beginTransaction();
h.setId(1);
h.setName("张三");
h.setWife(w);
session1.save(h);
session1.getTransaction().commit();
}
@AfterClass
public static void afterClass(){
sf.close();
}
}
观察结果:
1、观察hibernate生成的sql语句:
Hibernate: create table Husband (id integer not null, name varchar(255), wifeId integer, primary key (id))
Hibernate: create table Wife (id integer not null, name varchar(255), primary key (id))
Hibernate: alter table Husband add constraint FK_kruq9jfxa0jrc2od8dbh09mia foreign key (wifeId) references Wife (id)
2、如果不加@JoinColumn(name="wifeId"),在husband表将增加了关联wife表主键的外键wife_id。
3、使用sql语句:show create table husband;可以看到如下sql语句
CREATE TABLE `husband` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`wife_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_4565w2c4f1cj082spdiok0w6` (`wife_id`),
CONSTRAINT `FK_4565w2c4f1cj082spdiok0w6` FOREIGN KEY (`wife_id`) REFERENCES `wife` (`id`)
)
4、加入@JoinColumn(name="wifeId")可以修改husband表中外键的列的名称。再次运行show create table husband;可以看到如下片段
CREATE TABLE `husband` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`wifeId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_kruq9jfxa0jrc2od8dbh09mia` (`wifeId`),
CONSTRAINT `FK_kruq9jfxa0jrc2od8dbh09mia` FOREIGN KEY (`wifeId`) REFERENCES `wife` (`id`)
)
二、xml配置
在xml中配置one2one的方法如下:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>观察结果:
1.在xml中配置的是many-to-one和 unique="true"
第二种:一对一双向外键关联
一、Annotation配置
1.在一对一双向外键关联的时候,可以使用@OneToOne(mappedBy="对象名")的方式设置。
2.凡是双向关联,必设MappedBy
Husband.java
package com.zgy.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Husband {
private int id;
private String name;
private Wife wife;
@Id
@GeneratedValue
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;
}
@OneToOne
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
Wife.java
package com.zgy.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Wife {
private int id;
private String name;
private Husband husband;
@Id
@GeneratedValue
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;
}
@OneToOne(mappedBy="wife")
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
}
二、xml设置:
范例:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<one-to-one name="person"
property-ref="address"/>
</class>
第三种:一对一单向主键关联(不重要)
1.使用如下的Annotation设置一对一单项主键关联(目前没有成功过,Hibernate文档上所写,但是没有成功实现过)
@OneToOne
@PrimaryKeyJoinColumn
2.xml中的配置
<class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <one-to-one name="address"/></class> <class name="Address"> <id name="id" column="personId"> <generator class="foreign"> <param name="property">person</param> </generator> </id> <one-to-one name="person" constrained="true"/></class>
第四种:一对一双向主键关联(不重要)
第五种:一对一联合主键
一、Annotation配置
1.在wife.java中使用id和name作为联合主键
2.在husband.java中关联到wife.java中的联合主键
范例:
Wife.java
package com.zgy.hibernate.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
@Entity
@IdClass(WifePK.class)
public class Wife {
private int id;
private String name;
private int age;
@Id
public int getId() {
return id;
}
@Id
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Wife类的联合主键类WifePK.java
package com.bjsxt.hibernate;
import java.io.Serializable;
public class WifePK implements Serializable {
private int id;
private String name;
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;
}
}
Huaband.java
package com.zgy.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.OneToOne;
@Entity
public class Husband {
private int id;
private String name;
private Wife wife;
@Id
@GeneratedValue
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;
}
@OneToOne
@JoinColumns(
{
@JoinColumn(name="wifeId",referencedColumnName="id"),
@JoinColumn(name="wifeName",referencedColumnName="name")
}
)
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
观察结果:
1.在husband.java中,只使用@OneToOne,则会在husband表中自动生成wife_id ,wife_name两个字段。show create table husband;
CREATE TABLE `husband` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`wife_id` int(11) DEFAULT NULL,
`wife_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_4565w2c4f1cj082spdiok0w6` (`wife_id`),
CONSTRAINT `FK_4565w2c4f1cj082spdiok0w6` FOREIGN KEY (`wife_id`) REFERENCES `wife` (`id`)
)
2.使用@JoinColumns(
{
@JoinColumn(name="wifeId",referencedColumnName="id"),
@JoinColumn(name="wifeName",referencedColumnName="name")
}
)
可以自定义husband表中关联联合主键的列的名称
2.加入以上Annotation之后,再观察结果show create table husband;
CREATE TABLE `husband` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`wifeId` int(11) DEFAULT NULL,
`wifeName` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_sdshifqwgidsm93jdh5kfl0e4` (`wifeId`,`wifeName`),
CONSTRAINT `FK_sdshifqwgidsm93jdh5kfl0e4` FOREIGN KEY (`wifeId`, `wifeName`) REFERENCES `wife` (`id`, `name`)
)
第六种:组建映射
一、Annotation配置
1.不将wife.java作为实体类
2.在husband.java中,使用组件映射,关联wife.java中的wifeName和age
范例:
Wife.java中,不将wife作为是实体类
package com.zgy.hibernate.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
public class Wife {
private String wifeName;
private int age;
public String getWifeName() {
return wifeName;
}
public void setWifeName(String wifeName) {
this.wifeName = wifeName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Husband.java中使用@Embedded
package com.zgy.hibernate.model;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.OneToOne;
@Entity
public class Husband {
private int id;
private String name;
private Wife wife;
@Id
@GeneratedValue
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;
}
@Embedded
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
观察结果:
使用show create table husband;
CREATE TABLE `husband` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) NOT NULL,
`wifeName` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
二、xml配置
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zgy.hibernate.model">
<class name="Husband" table="husband">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<component name="wife">
<property name="wifeName"></property>
<property name="age"></property>
</component>
</class>
</hibernate-mapping>
第七种:多对一和一对多
A、多对一单向关联
例如:一个人可以有多个梦想
可以有如下设计
1、person表
2、dream表
错误的做法:
I、错误的做法:在一方加冗余
person表
personid | personname | dream |
1 | zhangsan | 1 |
1 | zhangsan | 2 |
dream表
dreamid | dreamdesc |
1 | Make money |
2 | Eat a lot |
正确的做法:
数据表设计:在多方加外键
一、Annotation配置
小实验1:
1.设计一个Group.java类,此类包含id、name属性
2.设计一个User.java类,此类包含id、name、group属性
3.对应关系:一个用于属于一个组,一个组含有多个用户,user对group的关系就是many-to-one单项映射关系
4.Group.java
package com.zgy.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_group")
public class Group {
private int id;
private String name;
@Id
@GeneratedValue
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;
}
}
5.User.java
package com.zgy.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
private int id;
private String name;
private Group group;
@Id
@GeneratedValue
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;
}
@ManyToOne
@JoinColumn(name="groupId")
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}
6.Junit测试
package com.zgy.hibernate.model;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class ORMapppingTest {
public static SessionFactory sf = null;
@BeforeClass
public static void beforeClass(){
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@Test
public void test() {
Group g = new Group();
User u = new User();
Session session1 = sf.openSession();
session1.beginTransaction();
g.setId(1);
g.setName("第一组");
session1.save(g);
session1.getTransaction().commit();
Session session2 = sf.openSession();
session2.beginTransaction();
u.setId(1);
u.setName("张三");
session2.save(u);
session2.getTransaction().commit();
}
@AfterClass
public static void afterClass(){
sf.close();
}
}
7.观察产生的sql语句:
Hibernate: create table t_group (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: create table t_user (id integer not null auto_increment, name varchar(255), groupId integer, primary key (id))
Hibernate: alter table t_user add constraint FK_7ktm6l2qkykpqrf6oq01ys8wy foreign key (groupId) references t_group (id)
二、xml配置
1.Group.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zgy.hibernate.model">
<class name="Group" table="t_group">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
</class>
</hibernate-mapping>
2.User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zgy.hibernate.model">
<class name="User" table="t_user">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<many-to-one name="group" column="groupId"></many-to-one>
</class>
</hibernate-mapping>
3.观察产生的sql语句
Hibernate: create table t_group (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: create table t_user (id integer not null auto_increment, name varchar(255), groupId integer, primary key (id))
Hibernate: alter table t_user add constraint FK_7ktm6l2qkykpqrf6oq01ys8wy foreign key (groupId) references t_group (id)
B、一对多单向关联
1.类:在一的一方存在多的一方的集合
2.数据库同上
一、Annotation配置
i、Group.java
package com.zgy.hibernate.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="t_group")
public class Group {
private int id;
private String name;
private Set<User> users = new HashSet<User>();
@Id
@GeneratedValue
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;
}
@OneToMany
@JoinColumn(name="groupId")
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
ii、User.java
package com.zgy.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
private int id;
private String name;
@Id
@GeneratedValue
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;
}
}
iii、观察生成的sql
Hibernate: create table t_group (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: create table t_user (id integer not null auto_increment, name varchar(255), groupId integer, primary key (id))
Hibernate: alter table t_user add constraint FK_7ktm6l2qkykpqrf6oq01ys8wy foreign key (groupId) references t_group (id)
二、Xml配置
i、Group.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zgy.hibernate.model">
<class name="Group" table="t_group">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<set name="users">
<key column="groupId"></key>
<one-to-many class="com.zgy.hibernate.model.User"/>
</set>
</class>
</hibernate-mapping>
ii、User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zgy.hibernate.model">
<class name="User" table="t_user">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
</class>
</hibernate-mapping>
iii、观察生成的sql语句
Hibernate: create table t_group (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: create table t_user (id integer not null auto_increment, name varchar(255), groupId integer, primary key (id))
Hibernate: alter table t_user add constraint FK_7ktm6l2qkykpqrf6oq01ys8wy foreign key (groupId) references t_group (id)
C、一对多,多对一双向关联
一、Annotation配置
1.Group.java
package com.zgy.hibernate.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="t_group")
public class Group {
private int id;
private String name;
private Set<User> users = new HashSet<User>();
@Id
@GeneratedValue
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;
}
@OneToMany(mappedBy="group")
@JoinColumn(name="groupId")
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
2.User.java
package com.zgy.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
private int id;
private String name;
private Group group;
@Id
@GeneratedValue
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;
}
@ManyToOne
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}
3.查看生成的sql语句
Hibernate: create table t_group (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: create table t_user (id integer not null auto_increment, name varchar(255), groupId integer, primary key (id))
Hibernate: alter table t_user add constraint FK_7ktm6l2qkykpqrf6oq01ys8wy foreign key (groupId) references t_group (id)
二、Xml配置
1.Group.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zgy.hibernate.model">
<class name="Group" table="t_group">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<set name="users">
<key column="groupId"></key>
<one-to-many class="com.zgy.hibernate.model.User"/>
</set>
</class>
</hibernate-mapping>
2.User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zgy.hibernate.model">
<class name="User" table="t_user">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<many-to-one name="group" column="groupId"></many-to-one>
</class>
</hibernate-mapping>
3.查看生成的sql语句
Hibernate: create table t_group (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: create table t_user (id integer not null auto_increment, name varchar(255), groupId integer, primary key (id))
Hibernate: alter table t_user add constraint FK_7ktm6l2qkykpqrf6oq01ys8wy foreign key (groupId) references t_group (id)
Hibernate——ORMapping