集合對映中的對映包(使用xml檔案)


如果持久類有List物件,我們可以通過列表或者bag元素在對映檔案中對映。 這個包(bag)就像List一樣,但它不需要索引元素。

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

我們來看看如何在對映檔案中實現包(bag):

<class name="com.yiibai.Question" table="q100">  
       ...        
          <bag name="answers" table="ans100">  
          <key column="qid"></key>  
          <element column="answer" type="string"></element>  
          </bag>  

       ...  
</class>

在集合對映中對映包的範例

在這個例子中,我們將學習包(bag)的集合對映的完整範例。 這是一個bag的例子,如果它儲存的值不是實體參照,這就是為什麼要使用element元素標籤來代替one-to-many。 如果您已經看過對映列表範例,這個範例與列表範例的所有情況下都是相同的,而是在檔案對映中使用bag而不是列表(list)。

建立一個Java專案:bagstring,完整的專案結構如下所示 -

1)建立持久類

這個持久化類定義了類的屬性,包括List。下面是 Question.java 檔案的程式碼 -

package com.yiibai;

import java.util.List;

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

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

2)建立持久化類的對映檔案

在這裡,我們建立用於定義列表的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="q101">
        <id name="id">
            <generator class="increment"></generator>
        </id>
        <property name="qname"></property>

        <bag name="answers" table="ans101">
            <key column="qid"></key>
            <element column="answer" type="string"></element>
        </bag>
    </class>

</hibernate-mapping>

3)建立組態檔案

此檔案包含有關資料庫和對映檔案的資訊。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" />

    </session-factory>

</hibernate-configuration>

4)建立儲存資料的類

建立一個類:MainTest.java用於執行測試資料,在這個類中儲存Question類的資料。

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

        ArrayList<String> list1 = new ArrayList<String>();
        list1.add("java is a programming language");
        list1.add("java is a platform");

        ArrayList<String> list2 = new ArrayList<String>();
        list2.add("Python is an Interface");
        list2.add("Python is an API");

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

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

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

        t.commit();

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

    }
}

如何獲取資料

在這裡,我們使用HQL來獲取Question類的所有記錄,包括答案。 在這種情況下,它從功能相關的兩個表中獲取資料。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
            List<String> list2 = q.getAnswers();
            Iterator<String> itr2 = list2.iterator();
            while (itr2.hasNext()) {
                System.out.println(itr2.next());
            }

        }
        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 21:14:36 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 max(id) from q101
Hibernate: insert into q101 (qname, id) values (?, ?)
Hibernate: insert into q101 (qname, id) values (?, ?)
Hibernate: insert into ans101 (qid, answer) values (?, ?)
Hibernate: insert into ans101 (qid, answer) values (?, ?)
Hibernate: insert into ans101 (qid, answer) values (?, ?)
Hibernate: insert into ans101 (qid, answer) 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 21:08:23 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 q101 question0_
Question Name: What is Java?
Hibernate: select answers0_.qid as qid1_0_0_, answers0_.answer as answer2_0_0_ from ans101 answers0_ where answers0_.qid=?
java is a programming language
java is a platform
Question Name: What is Python?
Hibernate: select answers0_.qid as qid1_0_0_, answers0_.answer as answer2_0_0_ from ans101 answers0_ where answers0_.qid=?
Python is an Interface
Python is an API
success