one-to-one外键双向关联之建表

首先是两个类,在中国是实行一夫一妻,也就是一个husband只能对应一个wife

Wife类,生成的表为t_wife

/**
 * 
 */
package com.maybe.test_1;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 * @author MayBe
 *
 * function:
 */
@Entity
@Table(name="t_wife")
public class Wife {
	
	private Integer id;
	private String name;
	private Husband husband;
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToOne
	public Husband getHusband() {
		return husband;
	}
	public void setHusband(Husband husband) {
		this.husband = husband;
	}
	
	
	

}
Husband类,生成的表为t_husband

/**
 * 
 */
package com.maybe.test_1;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 * @author MayBe
 *
 * function:
 */
@Entity
@Table(name="t_husband")
public class Husband {
	
	private Integer id;
	private String name;
	private Wife wife;
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	
	public String getName() {
		return name;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}

	@OneToOne
	public Wife getWife() {
		return wife;
	}

	public void setWife(Wife wife) {
		this.wife = wife;
	}
	

}
因为是一对一双向关联,wife类和husband类都包含对方的引用

hibernate.cfg.xml文件如下:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://localhost;DatabaseName=hibernate</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">yaphets</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
          
        <property name="current_session_context_class">thread</property>
      

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
       
        <property name="hbm2ddl.auto">update</property>
        
        <property name="format_sql">true</property>
        <!--  
        <mapping resource="com/maybe/test_1/Player.hbm.xml"/>
        
        -->

        <mapping class="com.maybe.test_1.Husband"/>
        <mapping class="com.maybe.test_1.Wife"/>
    </session-factory>

</hibernate-configuration>

用Junit进行测试,生成的建表语句如下所示

drop table t_wife

    create table t_husband (
        id int identity not null,
        name varchar(255),
        wife_id int,
        primary key (id)
    )

    create table t_wife (
        id int identity not null,
        name varchar(255),
        husband_id int,
        primary key (id)
    )

    alter table t_husband
        add constraint FK_hxagmw4p5aym8m63pxd6h2wwx
        foreign key (wife_id)
        references t_wife

    alter table t_wife
        add constraint FK_fi3kodkmubgryyblf4935y4dk
        foreign key (husband_id)
        references t_husband


不难看出这种一对一关系比较冗余,这个表的设计明显有问题,原因是我们在使用注解的时候One-to-one,是由hibernate自动生成的,解决办法非常简单,只要在注解上添加一个属性就可以,比如Husband类:

/**
 * 
 */
package com.maybe.test_1;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 * @author MayBe
 *
 * function:
 */
@Entity
@Table(name="t_husband")
public class Husband {
	
	private Integer id;
	private String name;
	private Wife wife;
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	
	public String getName() {
		return name;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}

	@OneToOne(mappedBy="husband")
	public Wife getWife() {
		return wife;
	}

	public void setWife(Wife wife) {
		this.wife = wife;
	}
	

}

我们在onetoone中使用了mappedby属性(在双向关联中一般都要用到),指明了mappedBy ,就指明了这个映射关系的承担方(Wife类中的husband),生成的数据表树下所示:

 create table t_husband (
        id int identity not null,
        name varchar(255),
        primary key (id)
    )

    create table t_wife (
        id int identity not null,
        name varchar(255),
        husband_id int,
        primary key (id)
    )

    alter table t_wife
        add constraint FK_fi3kodkmubgryyblf4935y4dk
        foreign key (husband_id)
        references t_husband

这次生成的表没有冗余,在wife表中有一个外键husband_id,正是通过husband_id来维护两个标的联系,所以双向关系中,要加上mappedBy属性确保数据库表的正确性

你可能感兴趣的:(java,Web,Hibernate)