hibernate实体xml一对多关系映射

Posted 小小小龙龙

tags:

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

单向一对多关系映射:

一个房间对应多个使用者,也就是Room實例知道User實例的存在,而User實例則沒有意識到Room實例。

用户表:

package onlyfun.caterpillar;

public class User {
    private Long id;
    private String name;
 
    public User() {}

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

  在room类中,是直接把用户作为一个对象直接存进room表中,其中user对象的类型是Set

room类:

package onlyfun.caterpillar; 

import java.util.Set;

public class Room { 
    private Long id; 
    private String address;
    private Set users;
 
    public Room() {}
 
    public Long getId() { 
        return id; 
    } 

    public void setId(Long id) { 
        this.id = id; 
    } 
 
    public String getAddress() { 
        return address; 
    }
 
    public void setAddress(String address) { 
        this.address = address; 
    }

    public Set getUsers() {
        return users;
    }

    public void setUsers(Set users) {
        this.users = users;
    } 
 
    public void addUser(User user) {
        users.add(user); 
    }
 
    public void removeUser(User user) {
        users.remove(user);
    }
}

  

User.hbm.xml

<?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="onlyfun.caterpillar.User" table="user"> 
        <id name="id" column="id"> 
            <generator class="native"/> 
        </id> 

        <property name="name" column="name"/> 
 
    </class> 

</hibernate-mapping>

  

使用<one-to- many>标签配置一对多:

Room.hbm.xml

<?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="onlyfun.caterpillar.Room" table="room"> 
        <id name="id" column="id"> 
            <generator class="native"/> 
        </id> 

        <property name="address" 
                  column="address" 
                  type="java.lang.String"/> 
 
        <set name="users" table="user" cascade="all">
            <key column="room_id"/>
            <one-to-many class="onlyfun.caterpillar.User"/>
        </set>
    </class> 

</hibernate-mapping>

  接著您可以如下儲存物件:

User user1 = new User();
user1.setName("bush"); 
        
User user2 = new User(); 
user2.setName("caterpillar"); 
        
User user3 = new User(); 
user3.setName("momor"); 

Room room1 = new Room(); 
room1.setUsers(new HashSet());
room1.setAddress("NTU-M8-419");
room1.addUser(user1);
room1.addUser(user2);
        
Room room2 = new Room();
room2.setUsers(new HashSet());
room2.setAddress("NTU-G3-302");
room2.addUser(user3);
        
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
        
session.save(room1);  // cascade 操作
session.save(room2); 
        
tx.commit();
session.close();



























以上是关于hibernate实体xml一对多关系映射的主要内容,如果未能解决你的问题,请参考以下文章

关联映射级联操作关系维护 ---- Hibernate之一对多|多对一关系

Hibernate5.x表与表之间的关系操作代码实现

(转)Hibernate框架基础——一对多关联关系映射

Hibernate—— 一对多 和 多对多关联关系映射(xml和注解)总结(转载)

学习笔记之Hibernate_映射关系之一对多映射关系

如何进行 Hibernate XML 映射,一对多使用 1 PK 映射到另一个具有复合键的实体