Hibernate一對多映射列表實例(使用xml文件)

如果持久化類具有包含實體引用的列表(List)對象,則需要使用一對多關聯來映射列表元素。

在這裏,我們使用論壇應用場景,在論壇中一個問題有多個答案。

在這種情況下,一個問題可以有多個答案,每個答案可能有自己的信息,這就是爲什麼在持久化類中使用列表(包含Answer類的引用)來表示一系列答案。

下面來看看看持久化類有列表對象(包含Answer類對象)。

package com.yiibai;  

import java.util.List;  

public class Question {  
    private int id;  
    private String qname;  
    private List<Answer> answers;  
    //getters and setters  

}

Question類有自己的信息,如idanswernamepostedBy

package com.yiibai;  

public class Answer {  
    private int id;  
    private String answername;  
    private String postedBy;  
    //getters and setters  

    }  
}

Question類具有包含實體引用的列表對象(即Answer類對象)。在這個示例中,需要使用一對多列表來映射此對象。 下面來看看如何映射它。

<list name="answers" cascade="all">  
    <key column="qid"></key>  
    <index column="type"></index>  
    <one-to-many class="com.yiibai.Answer"/>
</list>

List中的一對多個映射的完整示例

在這個例子中,我們將看到包含實體引用的映射列表的完整示例。創建一個項目:listonetomany,其完整的項目結構如下圖所示 -
Hibernate一對多映射列表實例(使用xml文件)

1)創建持久類

這個持久化類定義了類的屬性,包括List

Question.java

package com.yiibai;

import java.util.List;

public class Question {
    private int id;
    private String qname;
    private List<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 List<Answer> getAnswers() {
        return answers;
    }

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

}

Answer.java

package com.yiibai;

public class Answer {
    private int id;
    private String answername;
    private String postedBy;

    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 String toString() {
        return answername + " by: " + postedBy;
    }
}

2)創建持久化類的映射文件

在這裏,我們創建了用於定義列表(List)的question.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="com.yiibai.Question" table="q501">
        <id name="id">
            <generator class="increment"></generator>
        </id>
        <property name="qname"></property>

        <list name="answers" cascade="all">
            <key column="qid"></key>
            <index column="type"></index>
            <one-to-many class="com.yiibai.Answer" />
        </list>

    </class>

    <class name="com.yiibai.Answer" table="ans501">
        <id name="id">
            <generator class="increment"></generator>
        </id>
        <property name="answername"></property>
        <property name="postedBy"></property>
    </class>

</hibernate-mapping>

3)創建配置文件

文件:hibernate.hnm.cfg.xml包含有關數據庫和映射文件的信息。

4)創建存儲數據的類

在這個類中,我們存儲Question類的數據。MainTest.java文件的代碼如下 -

package com.yiibai;

import java.util.ArrayList;

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();

        Answer ans1 = new Answer();
        ans1.setAnswername("java is a programming language");
        ans1.setPostedBy("Ravi Su");

        Answer ans2 = new Answer();
        ans2.setAnswername("java is a platform");
        ans2.setPostedBy("Sudhir Wong");

        Answer ans3 = new Answer();
        ans3.setAnswername("Servlet is an Interface");
        ans3.setPostedBy("Jai Li");

        Answer ans4 = new Answer();
        ans4.setAnswername("Servlet is an API");
        ans4.setPostedBy("Arun");

        ArrayList<Answer> list1 = new ArrayList<Answer>();
        list1.add(ans1);
        list1.add(ans2);

        ArrayList<Answer> list2 = new ArrayList<Answer>();
        list2.add(ans3);
        list2.add(ans4);

        Question question1 = new Question();
        question1.setQname("What is Java?");
        question1.setAnswers(list1);

        Question question2 = new Question();
        question2.setQname("What is Servlet?");
        question2.setAnswers(list2);

        session.persist(question1);
        session.persist(question2);

        t.commit();
        session.close();
        System.out.println("success");

    }
}

執行上面代碼,輸出結果如下 -

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 08:54:12 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 ans501 (id integer not null, answername varchar(255), postedBy varchar(255), qid integer, type integer, primary key (id)) engine=InnoDB
Hibernate: create table q501 (id integer not null, qname varchar(255), primary key (id)) engine=InnoDB
Hibernate: alter table ans501 add constraint FKlha0fu9fjhckh66ivbt9wxn2p foreign key (qid) references q501 (id)
Hibernate: select max(id) from q501
Hibernate: select max(id) from ans501
Hibernate: insert into q501 (qname, id) values (?, ?)
Hibernate: insert into ans501 (answername, postedBy, id) values (?, ?, ?)
Hibernate: insert into ans501 (answername, postedBy, id) values (?, ?, ?)
Hibernate: insert into q501 (qname, id) values (?, ?)
Hibernate: insert into ans501 (answername, postedBy, id) values (?, ?, ?)
Hibernate: insert into ans501 (answername, postedBy, id) values (?, ?, ?)
Hibernate: update ans501 set qid=?, type=? where id=?
Hibernate: update ans501 set qid=?, type=? where id=?
Hibernate: update ans501 set qid=?, type=? where id=?
Hibernate: update ans501 set qid=?, type=? where id=?
success

如何獲取列表的數據

在這裏,我們使用HQL來獲取Question類的所有記錄,以及每個問題下面的回答。 在這種情況下,它從功能相關的兩個表中讀取數據。我們直接打印Question類的對象,需要在Answer類中覆蓋了toString()方法返回答案標題和發佈標題。 所以它打印答案名稱和發佈標題,而不是參考的ID。

FetchData.java 代碼如下所示 -

package com.yiibai;  
import java.util.*;  
import org.hibernate.*;  
import org.hibernate.cfg.*;  

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

    Session session=new Configuration().configure("hibernate.cfg.xml")  
                          .buildSessionFactory().openSession();  

    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  
        List<Answer> list2=q.getAnswers();  
        Iterator<Answer> itr2=list2.iterator();  
        while(itr2.hasNext()){  
            System.out.println(itr2.next());  
        }  

    }  
    session.close();  
    System.out.println("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 08:56:45 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 q501 question0_
Question Name: What is Java?
Hibernate: select answers0_.qid as qid4_0_0_, answers0_.id as id1_0_0_, answers0_.type as type5_0_, answers0_.id as id1_0_1_, answers0_.answername as answerna2_0_1_, answers0_.postedBy as postedBy3_0_1_ from ans501 answers0_ where answers0_.qid=?
java is a programming language by: Ravi Su
java is a platform by: Sudhir Wong
Question Name: What is Servlet?
Hibernate: select answers0_.qid as qid4_0_0_, answers0_.id as id1_0_0_, answers0_.type as type5_0_, answers0_.id as id1_0_1_, answers0_.answername as answerna2_0_1_, answers0_.postedBy as postedBy3_0_1_ from ans501 answers0_ where answers0_.qid=?
Servlet is an Interface by: Jai Li
Servlet is an API by: Arun
success