关系映射级别注解:
1.一对一单向外键
2.一对一双向外键关联
3.一对一单向外键联合主键
4.多对一单向外键关联
5.一对多单向外键关联
6.一对多双向外键关联
7.多对多单向外键关联
8.多对多双向外键关联
实体之间的映射关系:
一对一:一个公民对应一个身份证号码。
一对多(多对一):一个公民有多个银行账号。
多对多:一个学生有多个老师,一个老师有多个学生。
2.Hibernate关联映射注解之一对一双向外键关联
//主控方的配置同一对一单向外键关联
@OneToOne(mappedBy="idCard") //被控方
//双向关联,必须设置mappedBy属性。因为双向关联关系只能交给一方去控制,不可能在双方都设置外键保存关联关系,否则双方都无法保存。
1.配置文件
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="connection.username">root</property> <!-- 用户名 -->
<property name="connection.password">123456</property> <!-- 密码 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 驱动类名 -->
<property name="connection.url">jdbc:mysql:///hibernate?userUnicode=true&charaterEncoding=UTF-8</property><!-- 数据库url -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 方言 -->
<property name="show_sql">true</property> <!-- 在控制台显示SQL语句 -->
<property name="format_sql">true</property> <!-- 格式化SQL语句 -->
<property name="hibernate.default_schema">hibernate</property> <!-- 默认的数据库 -->
<property name="hbm2ddl.auto">update</property> <!-- 删除原表创建新表 -->
<property name="current_session_context_class">thread</property> <!-- 本地事务 -->
<!-- 注册Students,IdCard类-->
<mapping class="oto_bfk.Students"/>
<mapping class="oto_bfk.IdCard"/>
</session-factory>
</hibernate-configuration>
2.实体类
Students.java:
package oto_bfk;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; //JPA注解
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import org.hibernate.annotations.GenericGenerator;
@Entity
public class Students{
private IdCard idCard;
private int sid; // 学号
private String gender; // 性别
private Date birthday; // 出生日期
public Students() {
};
public Students(IdCard idCard, String gender, Date birthday) {
this.idCard = idCard;
this.gender = gender;
this.birthday = birthday;
}
@Id
@GeneratedValue //默认自增长
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="pid", unique=true) //设置外键
public IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
@Override
public String toString() {
return "Students [sid=" + sid + ", gender=" + gender + ", birthday=" + birthday
+ "]";
}
}
IdCard.java:
package oto_bfk;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import org.hibernate.annotations.GenericGenerator;
//省份证类
@Entity
public class IdCard {
@Id
@GeneratedValue(generator="pid")
@GenericGenerator(name="pid", strategy="assigned")
@Column(length=18)
private String pid;
private String sname;
@OneToOne(mappedBy="idCard") //双向关联交给Students控制
private Students stu;
public IdCard() {
}
public IdCard(String pid, String sname) {
this.pid = pid;
this.sname = sname;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Students getStu() {
return stu;
}
public void setStu(Students stu) {
this.stu = stu;
}
}
3.测试类
package oto_bfk;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class ObjectTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Test
public void testSaveStudentWithImage() throws Exception{
//创建配置对象
Configuration config = new Configuration().configure(); //hibernate.cfg.xml
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工产对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//会话对象
//session = sessionFactory.openSession();
//开启事务
//transaction = session.beginTransaction();
SchemaExport export = new SchemaExport(config);
export.create(true, true);
}
@Test
public void testSaveStudent() throws Exception{
//创建配置对象
Configuration config = new Configuration().configure(); //hibernate.cfg.xml
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工产对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//会话对象
session = sessionFactory.getCurrentSession();
//开启事务
transaction = session.beginTransaction();
//先生成身份证类对象
IdCard idCard = new IdCard("123456789012345678", "lise");
//再生成学生类对象
Students stu = new Students(idCard, "nv", new Date());
//先保存身份证类对象
session.save(idCard);
session.save(stu);
transaction.commit();
}
}