欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate4之集合类型映射(九)

程序员文章站 2022-04-14 09:17:41
...

hibernate集合类型映射

Hibernate4之集合类型映射(九)

1.HibernateUtil类,是session工厂类

package com.newbeedaly.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

    private static final SessionFactory sessionFactory=buildSessionFactory();

    private static SessionFactory buildSessionFactory(){
        Configuration configuration=new Configuration().configure(); // 实例化配置文件
        ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); // 实例化服务登记
        return configuration.buildSessionFactory(serviceRegistry); // 获取Session工厂
    }

    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
}

2.hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!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>

        <!--数据库连接设置 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>


        <!-- 方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- 控制台显示SQL -->
        <property name="show_sql">true</property>

        <!-- 自动更新表结构 -->
        <property name="hbm2ddl.auto">update</property>

        <mapping resource="com/newbeedaly/model/Student.hbm.xml"/>
        <mapping resource="com/newbeedaly/model/Student2.hbm.xml"/>
        <mapping resource="com/newbeedaly/model/Student3.hbm.xml"/>
        <mapping resource="com/newbeedaly/model/Student4.hbm.xml"/>


    </session-factory>

</hibernate-configuration>

3 set集合

package com.newbeedaly.model;

import java.util.Set;

public class Student {

    private long id;
    private String name;
    private Set<String> images;

    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;
    }
    public Set<String> getImages() {
        return images;
    }
    public void setImages(Set<String> images) {
        this.images = images;
    }




}
<?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.newbeedaly.model">

    <class name="Student" table="t_student">
        <id name="id" column="stuId">
            <generator class="native"></generator>
        </id>

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

        <set name="images" table="t_image">
            <key column="studentId"></key>
            <element column="imageName" type="string"></element>
        </set>
    </class>

</hibernate-mapping>

4.list集合

package com.newbeedaly.model;

import java.util.List;

public class Student2 {

    private long id;
    private String name;
    private List<String> images;

    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;
    }
    public List<String> getImages() {
        return images;
    }
    public void setImages(List<String> images) {
        this.images = images;
    }





}
<?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.newbeedaly.model">

    <class name="Student2" table="t_student">
        <id name="id" column="stuId">
            <generator class="native"></generator>
        </id>

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

        <list name="images" table="t_image2">
            <key column="studentId"></key>
            <list-index column="imageIndex"></list-index>
            <element column="imageName" type="string"></element>
        </list>
    </class>

</hibernate-mapping>

3.bag集合

package com.newbeedaly.model;

import java.util.List;

public class Student3 {

    private long id;
    private String name;
    private List<String> images;

    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;
    }
    public List<String> getImages() {
        return images;
    }
    public void setImages(List<String> images) {
        this.images = images;
    }





}
<?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.newbeedaly.model">

    <class name="Student3" table="t_student">
        <id name="id" column="stuId">
            <generator class="native"></generator>
        </id>

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

        <idbag name="images" table="t_image3">
            <collection-id type="long" column="imageId">
                <generator class="increment"></generator>
            </collection-id>
            <key column="studentId"></key>
            <element column="imageName" type="string"></element>
        </idbag>


    </class>

</hibernate-mapping>

4.map键值对

<?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.newbeedaly.model">

    <class name="Student3" table="t_student">
        <id name="id" column="stuId">
            <generator class="native"></generator>
        </id>

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

        <idbag name="images" table="t_image3">
            <collection-id type="long" column="imageId">
                <generator class="increment"></generator>
            </collection-id>
            <key column="studentId"></key>
            <element column="imageName" type="string"></element>
        </idbag>


    </class>

</hibernate-mapping>
<?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.newbeedaly.model">

    <class name="Student4" table="t_student">
        <id name="id" column="stuId">
            <generator class="native"></generator>
        </id>

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

        <map name="images" table="t_image4">
            <key column="studentId"></key>
            <map-key column="imageKey" type="string"></map-key>
            <element column="imageName" type="string"></element>
        </map>


    </class>

</hibernate-mapping>

Test类

package com.newbeedaly.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.newbeedaly.model.Student;
import com.newbeedaly.model.Student2;
import com.newbeedaly.model.Student3;
import com.newbeedaly.model.Student4;
import com.newbeedaly.util.HibernateUtil;

public class StudentTest {

    private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
    private Session session;

    @Before
    public void setUp() throws Exception {
        session=sessionFactory.openSession(); // 生成一个session
        session.beginTransaction(); // 开启事务
    }

    @After
    public void tearDown() throws Exception {
         session.getTransaction().commit(); // 提交事务
         session.close(); // 关闭session
    }

    @Test
    public void testSetSave(){
        Set<String> imageSet=new HashSet<String>();
        imageSet.add("image1.png");
        imageSet.add("image2.png");
        imageSet.add("image3.png");
        imageSet.add("image3.png");

        Student s1=new Student();
        s1.setImages(imageSet);
        session.save(s1);
    }

    @Test
    public void testSetFetch(){
        Student student=(Student)session.get(Student.class, Long.valueOf(1));
        Iterator it=student.getImages().iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }


    @Test
    public void testListSave(){
        List<String> imageList=new ArrayList<String>();
        imageList.add("image1.png");
        imageList.add("image2.png");
        imageList.add("image3.png");
        imageList.add("image3.png");

        Student2 s2=new Student2();
        s2.setImages(imageList);
        session.save(s2);
    }

    @Test
    public void testListFetch(){
        Student2 student2=(Student2)session.get(Student2.class, Long.valueOf(2));
        Iterator it=student2.getImages().iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }

    @Test
    public void testBagSave(){
        List<String> imageList=new ArrayList<String>();
        imageList.add("image1.png");
        imageList.add("image2.png");
        imageList.add("image3.png");
        imageList.add("image3.png");

        Student3 s3=new Student3();
        s3.setImages(imageList);
        session.save(s3);
    }

    @Test
    public void testBagFetch(){
        Student3 student3=(Student3)session.get(Student3.class, Long.valueOf(3));
        Iterator it=student3.getImages().iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }

    @Test
    public void testMapSave(){
        Map<String,String> imageMap=new HashMap<String,String>();
        imageMap.put("i1", "image1.png");
        imageMap.put("i2", "image2.png");
        imageMap.put("i3", "image3.png");
        imageMap.put("i4", "image4.png");

        Student4 s4=new Student4();
        s4.setImages(imageMap);
        session.save(s4);
    }

    @Test
    public void testMapFetch(){
        Student4 student4=(Student4)session.get(Student4.class, Long.valueOf(4));
        Map<String,String> imageMap=student4.getImages();
        Set keys=imageMap.keySet();
        Iterator it=keys.iterator();
        while(it.hasNext()){
            String key=(String)it.next();
            System.out.println(key+":"+imageMap.get(key));
        }
    }

}
相关标签: hibernate4