集合映射Set(使用xml文件)
如果持久類具有Set
對象,可以在映射文件中使用set
元素映射Set集合。 set
元素不需要索引元素。 List
和Set
之間的區別是: Set只存儲唯一的值。
我們來看看我們如何在映射文件中實現集合:
<class name="com.yiibai.Question" table="q102">
...
<set name="answers" table="ans102">
<key column="qid"></key>
<element column="answer" type="string"></element>
</set>
...
</class>
集合映射中映射集合的示例
在這個例子中,我們將看到set
的集合映射的完整示例。 這是存儲值不是實體引用的集合的示例,這就是爲什麼要使用 element
而不是one-to-many
。
在這裏,我們使用論壇的場景:論壇中一個問題有多個答案。
創建一個Java項目:setstring,完整的項目結果如下所示 -
1)創建持久化類
這個持久化類定義了包含Set
的類的屬性。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="q102">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="qname"></property>
<set name="answers" table="ans102">
<key column="qid"></key>
<element column="answer" type="string"></element>
</set>
</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)創建存儲數據的類
在這個類中,我們存儲Question
類的數據。MainTest.java
文件中的代碼如下所示 -
package com.yiibai;
import java.util.ArrayList;
import java.util.HashSet;
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();
HashSet<String> set1=new HashSet<String>();
set1.add("java is a programming language");
set1.add("java is a platform");
HashSet<String> set2=new HashSet<String>();
set2.add("Servlet is an Interface");
set2.add("Servlet is an API");
Question question1=new Question();
question1.setQname("What is Java?");
question1.setAnswers(set1);
Question question2=new Question();
question2.setQname("What is Servlet?");
question2.setAnswers(set2);
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
Set<String> set = q.getAnswers();
Iterator<String> itr2 = set.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 22:25:20 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 ans102 (qid integer not null, answer varchar(255)) engine=InnoDB
Hibernate: create table q102 (id integer not null, qname varchar(255), primary key (id)) engine=InnoDB
Hibernate: alter table ans102 add constraint FKce1yv9t7up0lavpmh8ry2gph5 foreign key (qid) references q102 (id)
Hibernate: select max(id) from q102
Hibernate: insert into q102 (qname, id) values (?, ?)
Hibernate: insert into q102 (qname, id) values (?, ?)
Hibernate: insert into ans102 (qid, answer) values (?, ?)
Hibernate: insert into ans102 (qid, answer) values (?, ?)
Hibernate: insert into ans102 (qid, answer) values (?, ?)
Hibernate: insert into ans102 (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 22:26:33 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 q102 question0_
Question Name: What is Java?
Hibernate: select answers0_.qid as qid1_0_0_, answers0_.answer as answer2_0_0_ from ans102 answers0_ where answers0_.qid=?
java is a programming language
java is a platform
Question Name: What is Servlet?
Hibernate: select answers0_.qid as qid1_0_0_, answers0_.answer as answer2_0_0_ from ans102 answers0_ where answers0_.qid=?
Servlet is an Interface
Servlet is an API
success