本教程每节课都附带源码,强烈大家建议配合源码学习。
本节源码: http://download.csdn.net/detail/e421083458/5253721
联合主键(Husband中存在wife的联合主键)
@JoinColumns
Wife类中建立联合主键,建立方式参考 ID生成策略中的联合主键部分
Husband类中写Wife对象属性,并在其get方法上写@OneToOne即可完成一对一外键映射
若想要指定生成的外键名 则需使用@JoinColumns注解,如下:
@OneToOne
@JoinColumns( { @JoinColumn(name = "wifeid", referencedColumnName = "id"),
@JoinColumn(name = "wifename", referencedColumnName = "name") })
/*@JoinColumns用于在一对一外键关联存在联合主键情况时指定生成的外键字段名称
@JoinColumns的参数为@JoinColumn数组 @JoinColumn内除需指定name属性外还需指定
referencedColumnName属性值 作用是可指定生成的字段名所对应的目标表字段名*/
public Wife getWife() {……}
项目源码:
Husband.java
package com.bjsxt.hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
@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;
}
}
Wife.java
package com.bjsxt.hibernate;
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;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Id
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
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;
}
}
测试脚本:
package com.bjsxt.hiberante;
import java.util.Date;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.Session;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.bjsxt.hibernate.Husband;
import com.bjsxt.hibernate.Wife;
public class HibernateQLTest {
private static SessionFactory sf;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("BeforeClass");
sf = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("AfterClass");
sf.close();
}
//
@Test
public void testSchemaExport(){
new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
}
@Test
public void testOneTOOne_Wife(){
Session session = sf.getCurrentSession();
session.beginTransaction();
Wife wife = new Wife();
wife.setName("wife");
Husband husband = new Husband();
husband.setName("husband");
husband.setWife(wife);
session.save(wife); //先保存妻子
session.save(husband); //再保存丈夫
session.getTransaction().commit();
}
public static void main(String args[]){
sf = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sf.getCurrentSession();
session.beginTransaction();
Wife wife = new Wife();
wife.setName("wife");
Husband husband = new Husband();
husband.setName("husband");
husband.setWife(wife);
session.save(wife); //先保存妻子
session.save(husband); //再保存丈夫
session.getTransaction().commit();
sf.close();
}
}