📅  最后修改于: 2021-01-02 15:40:06             🧑  作者: Mango
在本节中,我们将执行一对多关联以使用注释映射持久类的列表对象。
在这里,我们使用的是论坛的场景,其中一个问题有多个答案。
在这种情况下,一个问题可能有许多答案,每个答案可能都有自己的信息,这就是为什么我们在持久性类(包含Answer类的引用)中使用列表来表示答案的原因。
该持久类定义了包括List在内的类的属性。
Question.java
package com.javatpoint;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name="q5991")
public class Question {
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
private int id;
private String qname;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="qid")
@OrderColumn(name="type")
private List 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 getAnswers() {
return answers;
}
public void setAnswers(List answers) {
this.answers = answers;
}
}
Answer.java
package com.javatpoint;
import javax.persistence.*;
@Entity
@Table(name="ans5991")
public class Answer {
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
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;
}
}
打开pom.xml文件,然后单击源。现在,在
org.hibernate
hibernate-core
5.3.1.Final
com.oracle
ojdbc14
10.2.0.4.0
该文件包含有关数据库和映射文件的信息。
update
org.hibernate.dialect.Oracle9Dialect
jdbc:oracle:thin:@localhost:1521:xe
system
jtp
oracle.jdbc.driver.OracleDriver
在此类中,我们存储问题类别的数据。
package com.javatpoint;
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 list1=new ArrayList();
list1.add(ans1);
list1.add(ans2);
ArrayList list2=new ArrayList();
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");
}
}
注意-以类似的方式使用这些批注,我们还可以对set,map和bag对象执行一对多关联。
在这里,我们使用HQL来获取Question类的所有记录,包括答案。在这种情况下,它将从两个功能相关的表中获取数据。
package com.javatpoint;
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 list=query.getResultList();
Iterator itr=list.iterator();
while(itr.hasNext()){
Question q=itr.next();
System.out.println("Question Name: "+q.getQname());
//printing answers
List list2=q.getAnswers();
Iterator itr2=list2.iterator();
while(itr2.hasNext())
{
Answer a=itr2.next();
System.out.println(a.getAnswername()+":"+a.getPostedBy());
}
}
session.close();
System.out.println("success");
}
}