0.pom.xml
...... <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <hibernate-version>3.6.10.Final</hibernate-version> <javassist-version>3.12.1.GA</javassist-version> <slf4j-nop-version>1.6.6</slf4j-nop-version> <mysql-connector-version>5.1.21</mysql-connector-version> </properties> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate-version}</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>${javassist-version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>${slf4j-nop-version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-version}</version> </dependency> </dependencies> ......
1.schema.sql
alter table idcard2 drop foreign key FK62B3D2E778B7ECB9; drop table if exists idcard2; drop table if exists student2; create table idcard2 ( iid varchar(255) not null, num integer, student_id varchar(255) unique, primary key (iid) ); create table student2 ( sid varchar(255) not null, name varchar(255), primary key (sid) ); alter table idcard2 add index FK62B3D2E778B7ECB9 (student_id), add constraint FK62B3D2E778B7ECB9 foreign key (student_id) references student2 (sid);
2.Student2.java
package org.fool.model; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name = "student2") public class Student2 { @Id @GenericGenerator(name = "uuid", strategy = "uuid") @GeneratedValue(generator = "uuid") @Column(name = "sid") private String id; @Column(name = "name") private String name; @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "student") @Fetch(FetchMode.SELECT) private IdCard2 idCard; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public IdCard2 getIdCard() { return idCard; } public void setIdCard(IdCard2 idCard) { this.idCard = idCard; } }
以上的Annotation的就相当于以下的Student2.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="org.fool.model.Student2" table="student2"> <id name="id" column="sid" type="string"> <generator class="uuid"> </generator> </id> <property name="name" column="name" type="string" /> <one-to-one name="idCard" class="org.fool.model.IdCard2" cascade="all" fetch="select"></one-to-one> </class> </hibernate-mapping>
3.IdCard2.java
package org.fool.model; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name = "idcard2") public class IdCard2 { @Id @GenericGenerator(name = "uuid", strategy = "uuid") @GeneratedValue(generator = "uuid") @Column(name = "iid") private String id; @Column(name = "num") private int num; @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "student_id", unique = true) private Student2 student; public String getId() { return id; } public void setId(String id) { this.id = id; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public Student2 getStudent() { return student; } public void setStudent(Student2 student) { this.student = student; } }
以上的Annotation的就相当于以下的IdCard2.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="org.fool.model.IdCard2" table="idcard2"> <id name="id" column="iid" type="string"> <generator class="uuid"> </generator> </id> <property name="num" column="num" type="integer" /> <many-to-one name="student" class="org.fool.model.Student2" column="student_id" cascade="all" unique="true" lazy="false"></many-to-one> </class> </hibernate-mapping>
4.HibernateTest2.java
package org.fool.test; import org.fool.model.IdCard2; import org.fool.model.Student2; import org.fool.util.HibernateUtil; import org.hibernate.Session; import org.hibernate.Transaction; public class HibernateTest2 { public static void main(String[] args) { // insert(); // selectStudent(); // selectIdCard(); // updateStudent(); // delete(); } /** * 添加信息 */ public static void insert() { Session session = HibernateUtil.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Student2 student = new Student2(); student.setName("zhangsan"); IdCard2 idCard = new IdCard2(); idCard.setNum(123456); student.setIdCard(idCard); idCard.setStudent(student); session.save(student); tx.commit(); } catch (Exception e) { e.printStackTrace(); if (null != tx) { tx.rollback(); } } finally { HibernateUtil.closeSession(session); } } /** * 查询学生信息 */ public static void selectStudent() { Session session = HibernateUtil.openSession(); Transaction tx = null; Student2 student = null; try { tx = session.beginTransaction(); student = (Student2) session.get(Student2.class, "8aea139d397170d801397170d9480000"); tx.commit(); } catch (Exception e) { e.printStackTrace(); if (null != tx) { tx.rollback(); } } finally { HibernateUtil.closeSession(session); } System.out.println(student.getName()); } /** * 查询IdCard信息 */ public static void selectIdCard() { Session session = HibernateUtil.openSession(); Transaction tx = null; IdCard2 idCard = null; try { tx = session.beginTransaction(); idCard = (IdCard2) session.get(IdCard2.class, "8aea139d397170d801397170d9570001"); tx.commit(); } catch (Exception ex) { ex.printStackTrace(); if (null != tx) { tx.rollback(); } } finally { HibernateUtil.closeSession(session); } System.out.println(idCard.getNum()); System.out.println(idCard.getStudent().getName()); } /** * 更新学生信息 */ public static void updateStudent() { Session session = HibernateUtil.openSession(); Transaction tx = null; Student2 student = null; try { tx = session.beginTransaction(); student = (Student2) session.get(Student2.class, "8aea139d397170d801397170d9480000"); student.setName("lisi"); tx.commit(); } catch(Exception ex) { ex.printStackTrace(); if (null != tx) { tx.rollback(); } } finally { HibernateUtil.closeSession(session); } System.out.println(student.getName()); } /** * 级联删除 */ public static void delete() { Session session = HibernateUtil.openSession(); Transaction tx = null; IdCard2 idCard = null; try { tx = session.beginTransaction(); idCard = (IdCard2) session.get(IdCard2.class, "8aea139d397170d801397170d9570001"); session.delete(idCard); tx.commit(); } catch(Exception ex) { ex.printStackTrace(); if (null != tx) { tx.rollback(); } } finally { HibernateUtil.closeSession(session); } } }
5.插入一条数据后的结果
6.小结
外键关联:本质上是一对多的蜕化形式。将many-to-one元素中增加unique="true"属性,就变成了一对一。