Hibernate一对一单向外键关联(简单总结了5种方法)

比如一对夫妻,丈夫有id,name;妻子有id,name。增加一对一单向外键关联一般有以下几种方法:

1.在husband中增加一个外键,foreign key

2.在husband中增加字段wife_id,wife_id参照wife的id。以wife为主导,必须wife里有id才能参照

3.在wife中增加一个外键,foreign key

4.在wife中增加字段husband_id,husband_id参照husband的id。以hunsband为主导,必须hunsband里有id才能参照

5.增加中间表——关联表

数据库设计一般两种:主键关联,单向外键关联

示例代码:

husband类

package com.zzk.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 String getName() {
		return name;
	}

	@OneToOne
	public Wife getWife() {
		return wife;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}

}

wife类

package com.zzk.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
    @GeneratedValue
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}

}

JUNIT测试生成建表语句的方法:

	@Test
	public void testSchemaExport() {
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
	}

LOG4J输出:

11:07:55,366  INFO org.hibernate.tool.hbm2ddl.SchemaExport:226 - Running hbm2ddl schema export
11:07:55,369 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:242 - import file not found: /import.sql
11:07:55,369  INFO org.hibernate.tool.hbm2ddl.SchemaExport:251 - exporting generated schema to database
11:07:55,559 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    alter table Husband
        drop
        foreign key FKAEEA401B4AFE23AB
11:07:55,626 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:362 - Unsuccessful: alter table Husband drop foreign key FKAEEA401B4AFE23AB
11:07:55,626 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:363 - Table 'hibernate.husband' doesn't exist
11:07:55,627 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    drop table if exists Husband
11:07:55,648 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    drop table if exists Wife
11:07:55,652 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    create table Husband (
        id integer not null auto_increment,
        name varchar(255),
        wife_id integer,
        primary key (id)
    )
11:07:55,743 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    create table Wife (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )
11:07:55,818 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    alter table Husband
        add index FKAEEA401B4AFE23AB (wife_id),
        add constraint FKAEEA401B4AFE23AB
        foreign key (wife_id)
        references Wife (id)
11:07:56,040  INFO org.hibernate.tool.hbm2ddl.SchemaExport:268 - schema export complete

然后为了将wife_id改为wifeId,可以对husband类作如下处理:

package com.zzk.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 String getName() {
		return name;
	}

	@OneToOne
	@JoinColumn (name="wifeId")
	public Wife getWife() {
		return wife;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}

}

运行JUNIT测试方法,LOG4J打印的建表语句即变为:

11:27:11,113  INFO org.hibernate.tool.hbm2ddl.SchemaExport:226 - Running hbm2ddl schema export
11:27:11,116 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:242 - import file not found: /import.sql
11:27:11,116  INFO org.hibernate.tool.hbm2ddl.SchemaExport:251 - exporting generated schema to database
11:27:11,303 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    alter table Husband
        drop
        foreign key FKAEEA401BCC07428E
11:27:11,393 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:362 - Unsuccessful: alter table Husband drop foreign key FKAEEA401BCC07428E
11:27:11,394 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:363 - Error on rename of '.\hibernate\husband' to '.\hibernate\#sql2-8f0-f' (errno: 152)
11:27:11,395 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    drop table if exists Husband
11:27:11,418 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    drop table if exists Wife
11:27:11,445 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    create table Husband (
        id integer not null auto_increment,
        name varchar(255),
        wifeId integer,
        primary key (id)
    )
11:27:11,505 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    create table Wife (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )
11:27:11,579 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    alter table Husband
        add index FKAEEA401BCC07428E (wifeId),
        add constraint FKAEEA401BCC07428E
        foreign key (wifeId)
        references Wife (id)
11:27:11,715  INFO org.hibernate.tool.hbm2ddl.SchemaExport:268 - schema export complete

另外一种方法配置:

student类

package com.zzk.hibernate.model;

public class Student {

	private int id;
	private String name;

	private int age;
	private String sex;
	private boolean good;
	public boolean isGood() {
		return good;
	}
	public void setGood(boolean good) {
		this.good = good;
	}
	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 int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}

}

StuIdCard类

package com.zzk.hibernate.model;

public class StuIdCard {
    private int id;
    private String num;
    private Student student;

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}

}

StuIdCard.hbm.xml

<?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>
	<class name="com.zzk.hibernate.model.StuIdCard">
		<id name="id">
			<generator class="native"></generator>
		</id>

		<property name="num"/>
		<many-to-one name="student" column="studentId" unique="true"></many-to-one>
    </class>

</hibernate-mapping>

Student.hbm.xml

<?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>
	<class name="com.zzk.hibernate.model.StuIdCard">
		<id name="id">
			<generator class="native"></generator>
		</id>

		<property name="num"/>
		<many-to-one name="student" column="studentId" unique="true"></many-to-one>
    </class>

</hibernate-mapping>

hibernate.cfg.xml增加如下:

        <mapping resource="com/zzk/hibernate/model/Student.hbm.xml"/>
        <mapping resource="com/zzk/hibernate/model/StuIdCard.hbm.xml"/>

JUNIT单元测试:

	@Test
	public void testSchemaExport() {
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
	}

输出LOG4J的一些建表语句如下:

15:03:29,397  INFO org.hibernate.tool.hbm2ddl.SchemaExport:226 - Running hbm2ddl schema export
15:03:29,401 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:242 - import file not found: /import.sql
15:03:29,401  INFO org.hibernate.tool.hbm2ddl.SchemaExport:251 - exporting generated schema to database
15:03:31,082 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    alter table Husband
        drop
        foreign key FKAEEA401BCC07428E
15:03:31,880 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    alter table StuIdCard
        drop
        foreign key FKD3A449FF7E0CA1A0
15:03:31,934 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:362 - Unsuccessful: alter table StuIdCard drop foreign key FKD3A449FF7E0CA1A0
15:03:31,934 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:363 - Table 'hibernate.stuidcard' doesn't exist
15:03:31,934 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    drop table if exists Husband
15:03:31,960 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    drop table if exists StuIdCard
15:03:31,970 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    drop table if exists Student
15:03:32,045 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    drop table if exists Wife
15:03:32,132 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    create table Husband (
        id integer not null auto_increment,
        name varchar(255),
        wifeId integer,
        primary key (id)
    )
15:03:32,214 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    create table StuIdCard (
        id integer not null auto_increment,
        num varchar(255),
        studentId integer unique,
        primary key (id)
    )
15:03:32,306 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    create table Student (
        id integer not null auto_increment,
        name varchar(255),
        age integer,
        sex varchar(255),
        good char(1),
        primary key (id)
    )
15:03:32,382 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    create table Wife (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )
15:03:32,446 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    alter table Husband
        add index FKAEEA401BCC07428E (wifeId),
        add constraint FKAEEA401BCC07428E
        foreign key (wifeId)
        references Wife (id)
15:03:32,722 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -
    alter table StuIdCard
        add index FKD3A449FF7E0CA1A0 (studentId),
        add constraint FKD3A449FF7E0CA1A0
        foreign key (studentId)
        references Student (id)
15:03:32,916  INFO org.hibernate.tool.hbm2ddl.SchemaExport:268 - schema export complete
时间: 2024-10-22 20:33:41

Hibernate一对一单向外键关联(简单总结了5种方法)的相关文章

【hibernate框架】关系映射之一对一单向外键关联(XML实现)

在XML里面如何单向关联: 学生证与学生卡是一对一的关系,在学生证那一方做关联 Student.java: package cn.edu.hpu.model; public class Student { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String g

【hibernate框架】关系映射之一对一单项外键关联(Annotation实现)

一对一单向外键关联(Annotation做法): 例子,假设一夫配一妻(Husband与Wife).两个实体类的例子: Husband.java: package cn.edu.hpu.one2one; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity pu

【hibernate框架】一对一双向外键关联(Annotation实现)

一对一双向外键关联(Annotation方法): 一夫(Husband)一妻(Wife)的一对一双向外键关联 Husband和Wife实体类: package cn.edu.hpu.one2one; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import jav

hibernate 一对一注解-hibernate 一对一 唯一外键方式 注解,求大神帮忙?

问题描述 hibernate 一对一 唯一外键方式 注解,求大神帮忙? 例如: 有两张表: Husband(老公表):有字段:hid,hname Wife(老婆表):有字段:wid,wname,husbandid 老公和老婆是一对一,怎么配置一对一 唯一外键 注解,求助?

一对一注解-hibernate 一对一 唯一外键 方式的注解,求大神帮忙,?

问题描述 hibernate 一对一 唯一外键 方式的注解,求大神帮忙,? 例如: 有两张表: Husband(老公表):有字段:hid,hname Wife(老婆表):有字段:wid,wname,husbandid 老公和老婆是一对一,怎么配置一对一 唯一外键 注解,求助? 解决方案 给你推荐一篇博文把:http://www.aichengxu.com/view/38617 解决方案二: http://blog.csdn.net/sias1991/article/details/4661786

【hibernate框架】一对一双向外键关联(XML实现)

以Student与StuIDCard一对一双向关联为例,说一下用XML实现两者的一对一双向关联. 学生与学生证之间的关联关系. Student.java: package cn.edu.hpu.model; public class Student { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id

【hibernate框架】一对一单向主键关联(Annotation实现)

单项主键关联指的是:husband和wife之间有关联关联的.但是是用主键做关联的,说白了就是husband的id会参考wife的id(husband的id是1,n那么它所对应的wife的id也是1). Husband.java: package cn.edu.hpu.one2one; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; imp

系统学习hibernate之六:一对一外键关联映射双向关联

hibernate一对一唯一外键关联映射(双向关联Person<---->IdCard) 一对一唯一外键关联双向,需要在另一端(idcard),添加<one-to-one>标签,指示hibernate如何加载 其关联对象,默认根据主键加载person,外键关联映射中,因为两个实体采用的是person的外键维护的关系, 所以不能指定主键加载person,而要根据person的外键加载,所以采用如下映射方式: <one-to-one name="person"

系统学习hibernate之五:一对一外键关联

在hibernate中一对一外键关联跟多对一外键关联有很相似的地方, hibernate多对一外键关联先参考这个,然后只要是在*.hbm.xml里面加入以下代码: <many-to-one name="group" column="groupid" unique="true"/>, 就是加入unique="true"属性. 多对一外键关联中的*.hbm.xml里面加入以下代码:<many-to-one nam