閱讀764 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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"
        "https://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="com.zzk.hibernate.model.StuIdCard">
		<id name="id">
			<generator ></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"
        "https://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

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








最後更新:2017-04-02 17:28:39

  上一篇:go Android 手機上獲取手機當前上網IP地址(手機網關給手機號分配的IP)
  下一篇:go Service onStartCommand各種返回詳解,解決問題:隻調用onCreate不調用onStartCommand