一对一单向外键关联
一对一单向外键关联 分为Annotation和xml方式:
?
第一种:Annotation配置方式:丈夫和妻子? 在丈夫表里面有妻子的Id
?
package com.zchen.hibernate.oneTone;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToOne;import javax.persistence.Table;@Entity@Table(name="T_HUSBAND")public class Husband {private int id;private String name;private Wife wife;@OneToOne@JoinColumn(name="WIFE_ID")public Wife getWife() {return wife;}public void setWife(Wife wife) {this.wife = wife;}@Id@GeneratedValuepublic 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;}}
?
package com.zchen.hibernate.oneTone;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="T_WIFE")public class Wife {private int id;private String name;@Id@GeneratedValuepublic 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;}}
?
?
<?xml version="1.0"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql:///db_czbk_hibernate</property><property name="connection.username">root</property><property name="connection.password">1234</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="hbm2ddl.auto">create</property><property name="current_session_context_class">thread</property><property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property><property name="show_sql">true</property><mapping name="code">package com.zchen.hibernate.oneTone;public class Student {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;}}
?
package com.zchen.hibernate.oneTone;public class StudentIdCard {private int id;private String name;private Student student;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;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}}
?
package com.zchen.hibernate.oneTone;public class StudentIdCard {private int id;private String name;private Student student;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;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}}
?
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.zchen.hibernate.oneTone"> <class name="StudentIdCard" table="T_STUIDCARD"> <id name="id" column="ID"> <generator column="NAME"/> <many-to-one name="student" column="STUID" unique="true"></many-to-one> </class></hibernate-mapping>
?
?