Hibernate学习8—Hibernate 映射关系

Posted 有点懒惰的大青年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate学习8—Hibernate 映射关系相关的知识,希望对你有一定的参考价值。

第一节:Hibernate 一对一映射关系实现                    
假设一个用户对应一个地址;
 
1)按照主键映射:
User.java:
package com.cy.model;

public class User {
    private int id;
    private String name;
    private Address address;
    
    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 Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    
    
}    
View Code

Address.java:

package com.cy.model;

public class Address {
    private int id;
    private String address;
    private String zipcode;
    private User user;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getZipcode() {
        return zipcode;
    }
    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    
    
}
View Code

User.hbm.xml:

<hibernate-mapping package="com.cy.model">

    <class name="User" table="t_user">
        <id name="id" column="userId">
            <generator class="native"></generator>
        </id>
        <property name="name" column="userName"></property>
        
        <one-to-one name="address" class="com.cy.model.Address" cascade="all"></one-to-one>
    </class>

</hibernate-mapping>

Address.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.cy.model">
    
    <!-- 
        t_address表的主键和t_user表的主键一样;
        t_address表的id既做主键,又做外键。
     -->
    <class name="Address" table="t_address">
        <id name="id" column="addressId">
            <generator class="foreign">
                <param name="property">user</param>
            </generator>
        </id>
        <property name="address" column="address"></property>
        <property name="zipcode" column="zipcode"></property>
        
        <!-- 配置constrained="true"让t_address和t_user共享主键  -->
        <one-to-one name="user" class="com.cy.model.User" constrained="true"></one-to-one>
    </class>

</hibernate-mapping>

测试代码:

@Test
    public void testSave1(){
        User user=new User();
        user.setName("张三");
        
        Address address=new Address();
        address.setAddress("昭潭镇");
        address.setZipcode("247280");
        address.setUser(user);
        
        user.setAddress(address);
        session.save(user);
    }
View Code

结果:

t_address表结构:

t_address表的addressId既是主键,又是外键;
 
执行结果,查看数据库:

 

2)按照外键映射

User2.java:

package com.cy.model;

public class User2 {
    private int id;
    private String name;
    private Address2 address;
    
    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 Address2 getAddress() {
        return address;
    }
    public void setAddress(Address2 address) {
        this.address = address;
    }
    
    
}    
View Code

Address2.java:

package com.cy.model;

public class Address2 {
    private int id;
    private String address;
    private String zipcode;
    private User2 user;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getZipcode() {
        return zipcode;
    }
    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }
    public User2 getUser() {
        return user;
    }
    public void setUser(User2 user) {
        this.user = user;
    }
    
    
}
View Code

User2.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.cy.model">

    <class name="User2" table="t_user2">
        <id name="id" column="userId">
            <generator class="native"></generator>
        </id>
        <property name="name" column="userName"></property>
        
        <!-- 
            unique="true" 不能重复,唯一的;
            外键本来是可以重复的,但是通过unique="true"实现了唯一,这样就实现了一对一
         -->
        <many-to-one name="address" class="com.cy.model.Address2" column="addressId" cascade="all" unique="true"></many-to-one>
    </class>

</hibernate-mapping>
View Code

Address2.hbm.xml:

<hibernate-mapping package="com.cy.model">
    <class name="Address2" table="t_address2">
        <id name="id" column="addressId">
            <generator class="native"></generator>
        </id>
        <property name="address" column="address"></property>
        <property name="zipcode" column="zipcode"></property>
        
        <one-to-one name="user" class="com.cy.model.User2" property-ref="address"></one-to-one>
    </class>

</hibernate-mapping>
View Code

测试代码:

@Test
    public void testSave2(){
        User2 user=new User2();
        user.setName("李四");
        
        Address2 address=new Address2();
        address.setAddress("东至县昭潭镇");
        address.setZipcode("247280");
        address.setUser(user);
        
        user.setAddress(address);
        session.save(user);
    }
View Code

查看生辰的表:

t_user2表结构:

 

 

 

t_user2的外键addressId关联t_address2的主键;并且addressId存在唯一约束;

 

数据库中记录:

 

 

 

以上是关于Hibernate学习8—Hibernate 映射关系的主要内容,如果未能解决你的问题,请参考以下文章

hibernate--HelloWorld

Hibernate学习7—Hibernate 映射继承

hibernate学习(初识)

Hibernate学习———— 一对多映射关系

Hibernate4学习总结

oracle字段 Hibernate映射类型 java类型