我们可以使用列表, 集合, 包, 地图等来映射多对多关系。在这里, 我们将使用列表进行多对多映射。在这种情况下, 将创建三个表。
多对多映射示例
在此示例中, 我们将使用列表生成问题和答案之间的多对多关系。
1)创建持久性类
有两个持久性类Question.java和Answer.java。问题类包含答案类参考, 反之亦然。
Question.java
package com.srcmini;
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.srcmini;
import java.util.*;
public class Answer {
private int id;
private String answername;
private String postedBy;
private List<
Question>
questions;
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 List<
Question>
getQuestions() {
return questions;
}
public void setQuestions(List<
Question>
questions) {
this.questions = questions;
}
}
2)为持久化类创建Mapping文件
在这里, 我们创建了用来定义列表的question.hbm.xml和answer.hbm.xml文件。
question.hbm.xml
<
?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="com.srcmini.Question" table="ques1911">
<
id name="id" type="int">
<
column name="q_id" />
<
generator class="increment" />
<
/id>
<
property name="qname" />
<
list name="answers" table="ques_ans1911" fetch="select" cascade="all">
<
key column="q_id" />
<
index column="type">
<
/index>
<
many-to-many class="com.srcmini.Answer" column="ans_id" />
<
/list>
<
/class>
<
/hibernate-mapping>
answer.hbm.xml
<
?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="com.srcmini.Answer" table="ans1911">
<
id name="id" type="int">
<
column name="ans_id" />
<
generator class="increment" />
<
/id>
<
property name="answername"/>
<
property name="postedBy" />
<
/class>
<
/hibernate-mapping>
3)创建配置文件
该文件包含有关数据库和映射文件的信息。
hibernate.cfg.xml
<
?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">
<
hibernate-configuration>
<
session-factory>
<
property name="hbm2ddl.auto">
create<
/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"/>
<
mapping resource="answer.hbm.xml"/>
<
/session-factory>
<
/hibernate-configuration>
4)创建类来存储数据
【使用XML的Hibernate多对多示例】StoreData.java
package com.srcmini;
import java.util.ArrayList;
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 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");
Question q1=new Question();
q1.setQname("What is Java?");
ArrayList<
Answer>
l1=new ArrayList<
Answer>
();
l1.add(ans1);
l1.add(ans2);
q1.setAnswers(l1);
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");
Question q2=new Question();
q2.setQname("What is Servlet?");
ArrayList<
Answer>
l2=new ArrayList<
Answer>
();
l2.add(ans3);
l2.add(ans4);
q2.setAnswers(l2);
session.persist(q1);
session.persist(q2);
t.commit();
session.close();
System.out.println("success");
}
}
输出量
文章图片
文章图片
下载
推荐阅读
- 使用注解Hibernate多对一映射
- 使用注解Hibernate多对多示例
- Hibernate生命周期
- Hibernate配置
- Hibernate和Struts 2集成
- Hibernate和Spring集成
- HCQL(Hibernate标准查询语言)
- Hibernate组件映射
- Hibernate中的集合映射