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

hibernate一对一关联

时间:2016-04-19 00:01:11      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:

hibernate一对一主键关联

一对一主键关联指的是两个表通过主键形成的一对一映射。

数据表要求:A表的主键也是B表的主键同时B表的主键也是A表的外键

sql:

create table people(
id int primary key auto_increment,
name varchar(100) not null,
sex varchar(100) not null,
age int
)
create table idcard(
id int primary key,
idcard_code varchar(50) not null,
FOREIGN KEY(id) REFERENCES people(id) 
)

单向主键关联映射:

package com.demo.hibernate.beans;

public class IDcard {
    private int id;
    private String idcard_code;
    private People people;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getIdcard_code() {
        return idcard_code;
    }
    public void setIdcard_code(String idcardCode) {
        idcard_code = idcardCode;
    }
    public People getPeople() {
        return people;
    }
    public void setPeople(People people) {
        this.people = people;
    }
    public IDcard(int id, String idcardCode, People people) {
        super();
        this.id = id;
        idcard_code = idcardCode;
        this.people = people;
    }
    public IDcard(){}

}
package com.demo.hibernate.beans;

public class People {
    private int id;
    private String name;
    private String sex;
    private int age;
    //private IDcard idcard;
    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 String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    /*public IDcard getIdcard() {
        return idcard;
    }
    public void setIdcard(IDcard idcard) {
        this.idcard = idcard;
    }*/
    public People(int id, String name, String sex, int age) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    public People(){}

}
<?xml version="1.0" encoding="UTF-8"?>
<!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.demo.hibernate.beans.User" table="USER">
        <id name="id" type="int">
            <column name="id"></column><!-- 注意column要放在前面 -->
            <generator class="foreign">
                <param name="property">person</param>
            </generator>
        </id>
        
        <property name="idcard_code" type="string" column="IDCARD_CODE"/>
        
        <one-to-one name="prople" class="com.demo.hibernate.beans.People" cascade="all"></one-to-one>
        
    </class>
</hibernate-mapping>

 

hibernate一对一关联

标签:

原文地址:http://www.cnblogs.com/aigeileshei/p/5406257.html

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