码迷,mamicode.com
首页 > Web开发 > 详细

hibernate ——关联关系

时间:2016-06-02 00:29:43      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

1、一对一单向外键关联

技术分享
<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
                    <property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> -->

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/test?useUnicode=true&amp;characterEncoding=UTF-8</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="hbm2ddl.auto">update</property>
        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.MySQL5InnoDBDialect
        </property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
         <mapping class="com.pt.hibernate.Husband" />
         <mapping class="com.pt.hibernate.Wife" />
    </session-factory>

</hibernate-configuration>
hibernate.cfg.xml
技术分享
package com.pt.hibernate;

import java.util.Date;

import javax.annotation.Generated;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Husband {
    int id;
    String name;
    Date bornTime;
    Wife myWife ;
    
    @OneToOne
    @JoinColumn(name="wifiId")    //指定外键字段名
    public Wife getMyWife() {
        return myWife;
    }

    public void setMyWife(Wife myWife) {
        this.myWife = myWife;
    }

    @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;
    }

    public Date getBornTime() {
        return bornTime;
    }

    public void setBornTime(Date bornTime) {
        this.bornTime = bornTime;
    }

}
Husband.java
技术分享
package com.pt.hibernate;

import java.util.Date;

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

@Entity
public class Wife {
    int id;
    @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;
    }
    public Date getBornTime() {
        return bornTime;
    }
    public void setBornTime(Date bornTime) {
        this.bornTime = bornTime;
    }
    String name;
    Date bornTime;
}
Wife
技术分享
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class Test {
    public static void main(String[] arges){
        Configuration cfg = new Configuration();
        SessionFactory factory = cfg.configure().buildSessionFactory();
    }
}
Test

2、一对一双向外键关联

  两个相关联的实体类都写上@OneToOne即可,如果指定OneToOne的(mappedBy="")属性,这样就不会在两个表中都保留对方的ID,只会在其中一个表中保留对方的ID。只要有双向关联,该属性必设置。

技术分享
package com.pt.hibernate;

import java.util.Date;

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

@Entity
public class Wife {
    int id;
    String name;
    Date bornTime;
    Husband myHusband ;
    @OneToOne(mappedBy="myWife")
    public Husband getMyHusband() {
        return myHusband;
    }
    public void setMyHusband(Husband myHusband) {
        this.myHusband = myHusband;
    }
    @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;
    }
    public Date getBornTime() {
        return bornTime;
    }
    public void setBornTime(Date bornTime) {
        this.bornTime = bornTime;
    }

}
Wife

3、多对一单向外键关联

  多对一,数据库中的表现是在多的一方记录一的一方的ID,添加Son类,同时在配置文件中配置,Son类如下:

技术分享
package com.pt.hibernate;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Son {
    int id;
    String name;
    Husband myFather;
    
    @ManyToOne
    @JoinColumn(name="fatherId")
    public Husband getMyFather() {
        return myFather;
    }
    public void setMyFather(Husband myFather) {
        this.myFather = myFather;
    }
    @Id
    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;
    }
}
Son

 

4、多对一双向外键关联

   在3、的基础上,修改Husband类,如下:

技术分享
package com.pt.hibernate;

import java.util.Date;
import java.util.Set;

import javax.annotation.Generated;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity
public class Husband {
    int id;
    String name;
    Date bornTime;
    Wife myWife ;
    Set<Son> sons;
    
    @OneToMany(mappedBy="myFather")
    public Set<Son> getSons() {
        return sons;
    }

    public void setSons(Set<Son> sons) {
        this.sons = sons;
    }

    @OneToOne
    @JoinColumn(name="wifiId")    //指定外键字段名
    public Wife getMyWife() {
        return myWife;
    }

    public void setMyWife(Wife myWife) {
        this.myWife = myWife;
    }

    @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;
    }

    public Date getBornTime() {
        return bornTime;
    }

    public void setBornTime(Date bornTime) {
        this.bornTime = bornTime;
    }

}
Husband

 

hibernate ——关联关系

标签:

原文地址:http://www.cnblogs.com/tengpan-cn/p/5551323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!