首页 > 代码库 > hibernate 中集合的保存

hibernate 中集合的保存

一、开发流程

1)引入jar包,注意引入数据库驱动包

2)创建数据库表

//创建用户表
CREATE TABLE USER(
id INT PRIMARY KEY AUTO_INCREMENT,
uname VARCHAR(50),
ugender CHAR(2));
//创建地址表
CREATE TABLE address(
uid INT,
addr VARCHAR(200),
FOREIGN KEY (uid) REFERENCES USER(id));

3)创建实体类

import java.util.HashSet;
import java.util.Set;

public class User {
    private int id;
    private String uname;
    private String ugender;
    private Set<String> address = new HashSet<String>();
    
    public User() {
        // TODO Auto-generated constructor stub
    }

    public int getId() {
        return id;
    }

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

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUgender() {
        return ugender;
    }

    public void setUgender(String ugender) {
        this.ugender = ugender;
    }
    

    public Set<String> getAddress() {
        return address;
    }

    public void setAddress(Set<String> address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", uname=" + uname + ", ugender=" + ugender
                + "]";
    }
    
    

}

 

4)创建映射文件,user.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">

<!-- 

  This mapping demonstrates content-based discrimination for the
  table-per-hierarchy mapping strategy, using a formula
  discriminator.

-->

<hibernate-mapping 
    package="com.huitong.entity">
    
    <class name="User"  table="user">
        <id name="id" column="id">
            <generator class="native"/>
        </id>

        <property name="uname" column="uname"></property>
        <property name="ugender" column="ugender"></property>
        
        <set name="address" table="address">
            <key column="uid"></key>
            <element column="addr" type="string"></element>
        </set>

    </class>
</hibernate-mapping>

注意:地址栏因为是set集合,因此需要使用table。要指明外键和其他列的类型type,因为没有Bean类与其对应,因此要指明对象类型。

 

5) 配置hibernate.cfg.xml

<!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 name="UserFac">
        <!-- 1  对数据库连接进行配置 -->
        
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///day18</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>
        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        
        
        <!-- 2 其他设置 -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        
        
        <!-- 3 加载映射资源 -->
        <mapping resource="com/huitong/entity/User.hbm.xml"/>
    
    
        
    </session-factory>
</hibernate-configuration>

 

 

二、测试集合类

使用 HQL 获取全部元素

需要注意的是:Query q = session.createQuery("from User");

中from 表要是Bean 的类型。User而不是数据表user

hibernate 中集合的保存