Hibernate 的 集合Set映射一对多使用xml文件

如果持久化类设置了包含实体引用的对象,则需要使用一对多关联来映射集合(Set)元素。 我们可以通过任意一个Set集合来映射这个列表对象。

下面来看看看设置对象的持久化类。 在这种情况下,一个问题可以有很多答案,每个答案可能有自己的信息,这就是为什么使用set标签元素来表示一个答案(Answer类)集合。

Set映射一对多关系示例

在这个示例中,使用Set实现一对多和多对一关系映射。以论坛中的问题和答案为例,从一个问题可获取这个问题的所有回答(一对多),从一个问题反关联(多对一)得到问题信息。

创建一个名称为: setonetomany 的Java项目,其结构如下所示 -

Java代码

首先,将运行程序创建表并将对应数据插入到表中,然后再从表中读取数据。

Question.java 代码如下所示 -

package com.yiibai;

import java.util.Set;

public class Question {
    private int id;
    private String qname;
    private Set<Answer> answers;

    public int getId() {
        return id;
    }

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

    public String getQname() {
        return qname;
    }

    public void setQname(String qname) {
        this.qname = qname;
    }

    public Set<Answer> getAnswers() {
        return answers;
    }

    public void setAnswers(Set<Answer> answers) {
        this.answers = answers;
    }

}

Answer.java 代码如下所示 -

package com.yiibai;

public class Answer {
    private int id;  
    private String answername;  
    private String postedBy;  
    private int qid;
    private Question question;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getAnswername() {
        return answername;
    }
    public void setAnswername(String answername) {
        this.answername = answername;
    }
    public String getPostedBy() {
        return postedBy;
    }
    public void setPostedBy(String postedBy) {
        this.postedBy = postedBy;
    }
    public int getQid() {
        return qid;
    }
    public void setQid(int qid) {
        this.qid = qid;
    }
    public Question getQuestion() {
        return question;
    }
    public void setQuestion(Question question) {
        this.question = question;
    }

    public String toString(){
        return this.answername+", postby : "+this.postedBy;
    }

}

配置文件

一共有三个配置文件,它们分别如下:

answer.hnm.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="com.yiibai.Answer" table="a1002">
        <id name="id">
            <generator class="increment"></generator>
        </id>
        <property name="answername"></property>
        <property name="postedBy"></property>
        <many-to-one name="question" class="com.yiibai.Question" fetch="select">
            <column name="qid" not-null="true" />
        </many-to-one>
    </class>

</hibernate-mapping>

question.hnm.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="com.yiibai.Question" table="q1002">
        <id name="id">
            <generator class="increment"></generator>
        </id>
        <property name="qname"></property>

        <set name="answers" table="a1002"
                inverse="true" lazy="true" fetch="select">
            <key>
                <column name="qid" not-null="true" />
            </key>
            <one-to-many class="com.yiibai.Answer" />
        </set>

    </class>

</hibernate-mapping>

hibernate.cfg.xml 文件内容如下-

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

    <session-factory>
        <property name="hbm2ddl.auto">update</property>

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="show_sql">true</property>

        <mapping resource="question.hbm.xml" />
        <mapping resource="answer.hbm.xml" />
    </session-factory>

</hibernate-configuration>

运行示例

MainTest.java文件的代码如下 -

package com.yiibai;

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

import org.hibernate.*;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.*;

public class MainTest {
    public static void main(String[] args) {
        // 但在5.1.0版本汇总,hibernate则采用如下新方式获取:
        // 1. 配置类型安全的准服务注册类,这是当前应用的单例对象,不作修改,所以声明为final
        // 在configure("cfg/hibernate.cfg.xml")方法中,如果不指定资源路径,默认在类路径下寻找名为hibernate.cfg.xml的文件
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure("hibernate.cfg.xml").build();
        // 2. 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
        SessionFactory sessionFactory = new MetadataSources(registry)
                .buildMetadata().buildSessionFactory();

        /**** 上面是配置准备,下面开始我们的数据库操作 ******/
        Session session = sessionFactory.openSession();// 从会话工厂获取一个session

        // creating transaction object
        Transaction t = session.beginTransaction();

        Question question1 = new Question();
        question1.setQname("What is Java?");
        session.save(question1);

        Answer ans1 = new Answer();
        ans1.setAnswername("java is a programming language");
        ans1.setPostedBy("Ravi Su");
        ans1.setQuestion(question1);
        //question1.getAnswers().add(ans1);

        session.save(ans1);

        Answer ans2 = new Answer();
        ans2.setAnswername("java is a platform");
        ans2.setPostedBy("Sudhir Lee");
        ans2.setQuestion(question1);
        session.save(ans2);

        t.commit();
        session.close();

        System.out.println("success");
    }
}

FetchData.java文件的代码如下 -

package com.yiibai;

import java.util.*;

import org.hibernate.*;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.*;

public class FetchData {
    public static void main(String[] args) {

        // 但在5.1.0版本汇总,hibernate则采用如下新方式获取:
        // 1. 配置类型安全的准服务注册类,这是当前应用的单例对象,不作修改,所以声明为final
        // 在configure("cfg/hibernate.cfg.xml")方法中,如果不指定资源路径,默认在类路径下寻找名为hibernate.cfg.xml的文件
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure("hibernate.cfg.xml").build();
        // 2. 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
        SessionFactory sessionFactory = new MetadataSources(registry)
                .buildMetadata().buildSessionFactory();

        /**** 上面是配置准备,下面开始我们的数据库操作 ******/
        Session session = sessionFactory.openSession();// 从会话工厂获取一个session

        // creating transaction object
        Transaction t = session.beginTransaction();

        Query query = session.createQuery("from Question");
        List<Question> list = query.list();

        Iterator<Question> itr = list.iterator();
        while (itr.hasNext()) {
            Question q = itr.next();
            System.out.println("Question Name: " + q.getQname());

            // printing answers
            Set<Answer> set = q.getAnswers();
            Iterator<Answer> itr2 = set.iterator();
            while (itr2.hasNext()) {
                Answer an = (Answer)itr2.next();
                System.out.println(an.getAnswername());
            }

        }
        session.close();
        System.out.println("success");

    }
}

执行 MainTest.java得到以下结果 -

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Sun Mar 26 23:33:27 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Hibernate: create table a1002 (id integer not null, answername varchar(255), postedBy varchar(255), qid integer not null, primary key (id)) engine=InnoDB
Hibernate: create table q1002 (id integer not null, qname varchar(255), primary key (id)) engine=InnoDB
Hibernate: alter table a1002 add constraint FK9la7qmitcg24x22e3dh3q9kjv foreign key (qid) references q1002 (id)
Hibernate: alter table q1002 add constraint FK1ap2g6pq36pyrpjaw5o31yxdf foreign key (id) references q1002 (id)
Hibernate: select max(id) from q1002
Hibernate: select max(id) from a1002
Hibernate: insert into q1002 (qname, id) values (?, ?)
Hibernate: insert into a1002 (answername, postedBy, qid, id) values (?, ?, ?, ?)
Hibernate: insert into a1002 (answername, postedBy, qid, id) values (?, ?, ?, ?)
success

执行 FetchData.java得到以下结果 -

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Sun Mar 26 23:42:52 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Hibernate: select question0_.id as id1_1_, question0_.qname as qname2_1_ from q1002 question0_
Question Name: What is Java?
Hibernate: select answers0_.qid as qid4_0_0_, answers0_.id as id1_0_0_, answers0_.id as id1_0_1_, answers0_.answername as answerna2_0_1_, answers0_.postedBy as postedBy3_0_1_, answers0_.qid as qid4_0_1_ from a1002 answers0_ where answers0_.qid=?
java is a platform
java is a programming language
success

返回Hibernate教程,您可手机上继续学习Hibernate教程集合Set映射一对多使用xml文件语法,或扫下面二维码自动跳转。