Hibernate教程-Hibernate One to Many Example using XML
如果持久化类有包含实体引用的List对象,我们需要使用一对多关联来映射List元素。
这里,我们使用论坛的场景,其中一个问题有多个答案。
在这种情况下,一个问题可以有多个答案,每个答案可能有自己的信息,因此我们在持久化类中使用了list(包含Answer类的引用)来表示答案的集合。
让我们看看持久化类如何包含List对象(包含Answer类对象)。
package cn.javatiku;
import java.util.List;
public class Question {
private int id;
private String qname;
private List<Answer> answers;
// getters and setters
}
Answer类有自己的信息,如id、answername、postedBy等。
package cn.javatiku;
public class Answer {
private int id;
private String answername;
private String postedBy;
// getters and setters
}
Question类有一个包含实体引用的List对象(即Answer类对象)。在这种情况下,我们需要使用one-to-many关联来映射这个对象。让我们看看如何映射它。
<list name="answers" cascade="all">
<key column="qid"></key>
<index column="type"></index>
<one-to-many class="cn.javatiku.Answer"/>
</list>
一对多映射的完整示例(使用List)
在这个示例中,我们将看到包含实体引用的List映射的完整示例。
1) 创建持久化类
Question.java
package cn.javatiku;
import java.util.List;
public class Question {
private int id;
private String qname;
private List<Answer> answers;
// getters and setters
}
Answer.java
package cn.javatiku;
public class Answer {
private int id;
private String answername;
private String postedBy;
// getters and setters
}
2) 为持久化类创建映射文件
这里,我们创建了question.hbm.xml文件来定义List。
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">
<hibernate-mapping>
<class name="cn.javatiku.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="cn.javatiku.Answer"/>
</list>
</class>
<class name="cn.javatiku.Answer" table="ans501">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="answername"></property>
<property name="postedBy"></property>
</class>
</hibernate-mapping>
3) 创建配置文件
此文件包含有关数据库和映射文件的信息。
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">
<!-- 由MyEclipse Hibernate工具生成。 -->
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">jtp</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="question.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4) 创建存储数据的类
在这个类中,我们存储了Question类的数据。
package cn.javatiku;
import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class StoreData {
public static void main(String[] args) {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
SessionFactory factory = meta.getSessionFactoryBuilder().build();
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Answer ans1 = new Answer();
ans1.setAnswername("Java is a programming language");
ans1.setPostedBy("Ravi Malik");
Answer ans2 = new Answer();
ans2.setAnswername("Java is a platform");
ans2.setPostedBy("Sudhir Kumar");
Answer ans3 = new Answer();
ans3.setAnswername("Servlet is an Interface");
ans3.setPostedBy("Jai Kumar");
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");
}
}
输出
如何获取List的数据
这里,我们使用HQL来获取Question类的所有记录,包括答案。在这种情况下,它从功能依赖的两个表中获取数据。这里,我们直接打印Answer类的对象,但我们在Answer类中重写了toString()方法,返回answername和poster name。因此,它打印答案名称和发布者名称,而不是引用id。
FetchData.java
package cn.javatiku;
import java.util.*;
import javax.persistence.TypedQuery;
import org.hibernate.*;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class FetchData {
public static void main(String[] args) {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
SessionFactory factory = meta.getSessionFactoryBuilder().build();
Session session = factory.openSession();
TypedQuery query = session.createQuery("from Question");
List<Question> list = query.getResultList();
Iterator<Question> itr = list.iterator();
while (itr.hasNext()) {
Question q = itr.next();
System.out.println("Question Name: " + q.getQname());
// 打印答案
List<Answer> list2 = q.getAnswers();
Iterator<Answer> itr2 = list2.iterator();
while (itr2.hasNext()) {
Answer a = itr2.next();
System.out.println(a.getAnswername() + ": " + a.getPostedBy());
}
}
session.close();
System.out.println("success");
}
}