Contents
Thủ Thuật Hướng dẫn Hibernate list Chi Tiết
Bạn đang tìm kiếm từ khóa Hibernate list được Update vào lúc : 2022-11-25 21:07:09 . Với phương châm chia sẻ Kinh Nghiệm Hướng dẫn trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi đọc tài liệu vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Mình lý giải và hướng dẫn lại nha.
List in collection:
A List represents an ordered or sequenced group of elements. It may contain duplicate elements.
Note: Elements can be inserted or retrieved by their position in the list. Position or index value starts from 0. List interface defines its own methods in addition to the methods of collection interface.
Nội dung chính
- List in collection:List mapping:
List mapping:
If an entity has a list of values for a property then this property can be mapped by using element. A list is initialized with java.util.ArrayList.
Example:
Student.java
import java.util.List;
/**
* This class represents a persistent class for Student.
* @author w3spoint
*/
public class Student
//data members
private int studentId;
private String firstName;
private String lastName;
private String className;
private String rollNo;
private int age;
private List subjects;
//no-argument constructor
public Student()
//argument constructor
public Student(String firstName, String lastName,
String className, String rollNo, int age)
this.firstName = firstName;
this.lastName = lastName;
this.className = className;
this.rollNo = rollNo;
this.age = age;
//getter and setter methods
public int getStudentId()
return studentId;
public void setStudentId(int studentId)
this.studentId = studentId;
public String getFirstName()
return firstName;
public void setFirstName(String firstName)
this.firstName = firstName;
public String getLastName()
return lastName;
public void setLastName(String lastName)
this.lastName = lastName;
public String getClassName()
return className;
public void setClassName(String className)
this.className = className;
public String getRollNo()
return rollNo;
public void setRollNo(String rollNo)
this.rollNo = rollNo;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
public List getSubjects()
return subjects;
public void setSubjects(List subjects)
this.subjects = subjects;
import java.util.List; /** * This class represents a persistent class for Student. * @author w3spoint */ public class Student //data members private int studentId; private String firstName; private String lastName; private String className; private String rollNo; private int age; private List subjects; //no-argument constructor public Student() //argument constructor public Student(String firstName, String lastName, String className, String rollNo, int age) this.firstName = firstName; this.lastName = lastName; this.className = className; this.rollNo = rollNo; this.age = age; //getter and setter methods public int getStudentId() return studentId; public void setStudentId(int studentId) this.studentId = studentId; public String getFirstName() return firstName; public void setFirstName(String firstName) this.firstName = firstName; public String getLastName() return lastName; public void setLastName(String lastName) this.lastName = lastName; public String getClassName() return className; public void setClassName(String className) this.className = className; public String getRollNo() return rollNo; public void setRollNo(String rollNo) this.rollNo = rollNo; public int getAge() return age; public void setAge(int age) this.age = age; public List getSubjects() return subjects; public void setSubjects(List subjects) this.subjects = subjects;
Subject.java
/**
* This class represents a persistent class for Subject.
* @author w3spoint
*/
public class Subject
//data members
private int subjectId;
private String subjectName;
//no argument constructor
public Subject()
//argument constructor
public Subject(String subjectName)
this.subjectName = subjectName;
//getter and setter methods
public int getSubjectId()
return subjectId;
public void setSubjectId(int subjectId)
this.subjectId = subjectId;
public String getSubjectName()
return subjectName;
public void setSubjectName(String subjectName)
this.subjectName = subjectName;
/** * This class represents a persistent class for Subject. * @author w3spoint */ public class Subject //data members private int subjectId; private String subjectName; //no argument constructor public Subject() //argument constructor public Subject(String subjectName) this.subjectName = subjectName; //getter and setter methods public int getSubjectId() return subjectId; public void setSubjectId(int subjectId) this.subjectId = subjectId; public String getSubjectName() return subjectName; public void setSubjectName(String subjectName) this.subjectName = subjectName;
hibernate.cfg.xml
org.hibernate.dialect.OracleDialect
jdbc:oracle:thin:@localhost:1521:XE
system
oracle
oracle.jdbc.driver.OracleDriver
update
true
org.hibernate.dialect.OracleDialect jdbc:oracle:thin:@localhost:1521:XE system oracle oracle.jdbc.driver.OracleDriver update true
student.hbm.xml
subject.hbm.xml
HibernateUtil.java
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* This is a utility class for getting the hibernate session object.
* @author w3spoint
*/
public class HibernateUtil
private static final SessionFactory sessionFactory =
buildSessionFactory();
private static SessionFactory buildSessionFactory()
SessionFactory sessionFactory = null;
try
//Create the configuration object.
Configuration configuration = new Configuration();
//Initialize the configuration object
//with the configuration file data
configuration.configure(“hibernate.cfg.xml”);
// Get the SessionFactory object from configuration.
sessionFactory = configuration.buildSessionFactory();
catch (Exception e)
e.printStackTrace();
return sessionFactory;
public static SessionFactory getSessionFactory()
return sessionFactory;
import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * This is a utility class for getting the hibernate session object. * @author w3spoint */ public class HibernateUtil private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() SessionFactory sessionFactory = null; try //Create the configuration object. Configuration configuration = new Configuration(); //Initialize the configuration object //with the configuration file data configuration.configure(“hibernate.cfg.xml”); // Get the SessionFactory object from configuration. sessionFactory = configuration.buildSessionFactory(); catch (Exception e) e.printStackTrace(); return sessionFactory; public static SessionFactory getSessionFactory() return sessionFactory;
HibernateTest.java
import java.util.ArrayList;
import com.w3spoint.persistence.StudentDBOperations;
/**
* This class is used for the hibernate operations.
* @author w3spoint
*/
public class HibernateTest
public static void main(String args[])
ArrayList subjectList1 = new ArrayList();
subjectList1.add(new Subject(“Data Structure”));
subjectList1.add(new Subject(“Operting System”));
ArrayList subjectList2 = new ArrayList();
subjectList2.add(new Subject(“Compier”));
subjectList2.add(new Subject(“Networking”));
subjectList2.add(new Subject(“DBMS”));
//Create the student object.
Student student1 = new Student(“Harish”, “Kansal”,
“MCA final”, “MCA/07/72”, 27);
Student student2 = new Student(“Sunil”, “Kumar”,
“MCA final”, “MCA/07/73”, 32);
student1.setSubjects(subjectList1);
student2.setSubjects(subjectList2);
StudentDBOperations obj = new StudentDBOperations();
//insert student object.
obj.addStudent(student1);
obj.addStudent(student2);
//show all student object.
obj.showAllStudentDetails();
import java.util.ArrayList; import com.w3spoint.persistence.StudentDBOperations; /** * This class is used for the hibernate operations. * @author w3spoint */ public class HibernateTest public static void main(String args[]) ArrayList subjectList1 = new ArrayList(); subjectList1.add(new Subject(“Data Structure”)); subjectList1.add(new Subject(“Operting System”)); ArrayList subjectList2 = new ArrayList(); subjectList2.add(new Subject(“Compier”)); subjectList2.add(new Subject(“Networking”)); subjectList2.add(new Subject(“DBMS”)); //Create the student object. Student student1 = new Student(“Harish”, “Kansal”, “MCA final”, “MCA/07/72”, 27); Student student2 = new Student(“Sunil”, “Kumar”, “MCA final”, “MCA/07/73”, 32); student1.setSubjects(subjectList1); student2.setSubjects(subjectList2); StudentDBOperations obj = new StudentDBOperations(); //insert student object. obj.addStudent(student1); obj.addStudent(student2); //show all student object. obj.showAllStudentDetails();
StudentDBOperations.java
import java.util.List;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.w3spoint.business.Student;
import com.w3spoint.business.Subject;
/**
* This class contains the methods to interact with database.
* @author w3spoint
*/
public class StudentDBOperations
/**
* This method is used to insert a new student record.
* @param student
* @return studentId
* @author w3spoint
*/
public Integer addStudent(Student student)
Transaction tx = null;
Integer studentId = null;
//Get the session object.
Session session =
HibernateUtil.getSessionFactory().openSession();
try
tx = session.beginTransaction();
studentId = (Integer) session.save(student);
tx.commit();
catch (HibernateException e)
if(tx!=null)
tx.rollback();
e.printStackTrace();
finally
session.close();
return studentId;
/**
* This method is used retrieve and show the records.
* @author w3spoint
*/
public void showAllStudentDetails()
Transaction tx = null;
//Get the session object.
Session session =
HibernateUtil.getSessionFactory().openSession();
try
tx = session.beginTransaction();
List students =
session.createQuery(“FROM Student”).list();
for(Student student : students)
System.out.println(“First Name: ”
+ student.getFirstName());
System.out.println(“Last Name: ”
+ student.getLastName());
System.out.println(“Class: ”
+ student.getClassName());
System.out.println(“RollNo: ”
+ student.getRollNo());
System.out.println(“Age: ”
+ student.getAge());
List subjects =
student.getSubjects();
for(Subject subject : subjects)
System.out.println(“Subject Name:”
+ subject.getSubjectName());
tx.commit();
catch (HibernateException e)
if(tx!=null)
tx.rollback();
e.printStackTrace();
finally
session.close();
import java.util.List; import java.util.Set; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import com.w3spoint.business.Student; import com.w3spoint.business.Subject; /** * This class contains the methods to interact with database. * @author w3spoint */ public class StudentDBOperations /** * This method is used to insert a new student record. * @param student * @return studentId * @author w3spoint */ public Integer addStudent(Student student) Transaction tx = null; Integer studentId = null; //Get the session object. Session session = HibernateUtil.getSessionFactory().openSession(); try tx = session.beginTransaction(); studentId = (Integer) session.save(student); tx.commit(); catch (HibernateException e) if(tx!=null) tx.rollback(); e.printStackTrace(); finally session.close(); return studentId; /** * This method is used retrieve and show the records. * @author w3spoint */ public void showAllStudentDetails() Transaction tx = null; //Get the session object. Session session = HibernateUtil.getSessionFactory().openSession(); try tx = session.beginTransaction(); List students = session.createQuery(“FROM Student”).list(); for(Student student : students) System.out.println(“First Name: ” + student.getFirstName()); System.out.println(“Last Name: ” + student.getLastName()); System.out.println(“Class: ” + student.getClassName()); System.out.println(“RollNo: ” + student.getRollNo()); System.out.println(“Age: ” + student.getAge()); List subjects = student.getSubjects(); for(Subject subject : subjects) System.out.println(“Subject Name:” + subject.getSubjectName()); tx.commit(); catch (HibernateException e) if(tx!=null) tx.rollback(); e.printStackTrace(); finally session.close();
Output:
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Student (First_Name, Last_Name,
Class, RollNo, Age, Student_Id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Subject (Subject_Name, Subject_Id) values (?, ?)
Hibernate: insert into Subject (Subject_Name, Subject_Id) values (?, ?)
Hibernate: update Subject set Student_Id=?, idx=? where Subject_Id=?
Hibernate: update Subject set Student_Id=?, idx=? where Subject_Id=?
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Student (First_Name, Last_Name,
Class, RollNo, Age, Student_Id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Subject (Subject_Name, Subject_Id) values (?, ?)
Hibernate: insert into Subject (Subject_Name, Subject_Id) values (?, ?)
Hibernate: insert into Subject (Subject_Name, Subject_Id) values (?, ?)
Hibernate: update Subject set Student_Id=?, idx=? where Subject_Id=?
Hibernate: update Subject set Student_Id=?, idx=? where Subject_Id=?
Hibernate: update Subject set Student_Id=?, idx=? where Subject_Id=?
Hibernate: select student0_.Student_Id as Student1_0_,
student0_.First_Name as First2_0_, student0_.Last_Name as
Last3_0_, student0_.Class as Class0_, student0_.RollNo as
RollNo0_, student0_.Age as Age0_ from Student student0_
First Name: Harish
Last Name: Kansal
Class: MCA final
RollNo: MCA/07/72
Age: 27
Hibernate: select subjects0_.Student_Id as Student3_1_,
subjects0_.Subject_Id as Subject1_1_, subjects0_.idx as idx1_,
subjects0_.Subject_Id as Subject1_1_0_, subjects0_.Subject_Name
as Subject2_1_0_ from Subject subjects0_ where subjects0_.Student_Id=?
Subject Name:Data Structure
Subject Name:Operting System
First Name: Sunil
Last Name: Kumar
Class: MCA final
RollNo: MCA/07/73
Age: 32
Hibernate: select subjects0_.Student_Id as Student3_1_,
subjects0_.Subject_Id as Subject1_1_, subjects0_.idx as idx1_,
subjects0_.Subject_Id as Subject1_1_0_, subjects0_.Subject_Name
as Subject2_1_0_ from Subject subjects0_ where subjects0_.Student_Id=?
Subject Name:Compier
Subject Name:Networking
Subject Name:DBMS
Hibernate: select hibernate_sequence.nextval from dual Hibernate: select hibernate_sequence.nextval from dual Hibernate: select hibernate_sequence.nextval from dual Hibernate: insert into Student (First_Name, Last_Name, Class, RollNo, Age, Student_Id) values (?, ?, ?, ?, ?, ?) Hibernate: insert into Subject (Subject_Name, Subject_Id) values (?, ?) Hibernate: insert into Subject (Subject_Name, Subject_Id) values (?, ?) Hibernate: update Subject set Student_Id=?, idx=? where Subject_Id=? Hibernate: update Subject set Student_Id=?, idx=? where Subject_Id=? Hibernate: select hibernate_sequence.nextval from dual Hibernate: select hibernate_sequence.nextval from dual Hibernate: select hibernate_sequence.nextval from dual Hibernate: select hibernate_sequence.nextval from dual Hibernate: insert into Student (First_Name, Last_Name, Class, RollNo, Age, Student_Id) values (?, ?, ?, ?, ?, ?) Hibernate: insert into Subject (Subject_Name, Subject_Id) values (?, ?) Hibernate: insert into Subject (Subject_Name, Subject_Id) values (?, ?) Hibernate: insert into Subject (Subject_Name, Subject_Id) values (?, ?) Hibernate: update Subject set Student_Id=?, idx=? where Subject_Id=? Hibernate: update Subject set Student_Id=?, idx=? where Subject_Id=? Hibernate: update Subject set Student_Id=?, idx=? where Subject_Id=? Hibernate: select student0_.Student_Id as Student1_0_, student0_.First_Name as First2_0_, student0_.Last_Name as Last3_0_, student0_.Class as Class0_, student0_.RollNo as RollNo0_, student0_.Age as Age0_ from Student student0_ First Name: Harish Last Name: Kansal Class: MCA final RollNo: MCA/07/72 Age: 27 Hibernate: select subjects0_.Student_Id as Student3_1_, subjects0_.Subject_Id as Subject1_1_, subjects0_.idx as idx1_, subjects0_.Subject_Id as Subject1_1_0_, subjects0_.Subject_Name as Subject2_1_0_ from Subject subjects0_ where subjects0_.Student_Id=? Subject Name:Data Structure Subject Name:Operting System First Name: Sunil Last Name: Kumar Class: MCA final RollNo: MCA/07/73 Age: 32 Hibernate: select subjects0_.Student_Id as Student3_1_, subjects0_.Subject_Id as Subject1_1_, subjects0_.idx as idx1_, subjects0_.Subject_Id as Subject1_1_0_, subjects0_.Subject_Name as Subject2_1_0_ from Subject subjects0_ where subjects0_.Student_Id=? Subject Name:Compier Subject Name:Networking Subject Name:DBMS
Download this example.
Next Topic: Hibernate bag mapping with example.
Previous Topic: Hibernate SortedSet mapping with example.
Please Share
://.youtube/watch?v=KQjAY-XLgHs
Video Hibernate list ?
Bạn vừa đọc nội dung bài viết Với Một số hướng dẫn một cách rõ ràng hơn về Review Hibernate list tiên tiến và phát triển nhất
Chia Sẻ Link Cập nhật Hibernate list miễn phí
Người Hùng đang tìm một số trong những ShareLink Download Hibernate list miễn phí.
Giải đáp vướng mắc về Hibernate list
Nếu sau khi đọc nội dung bài viết Hibernate list vẫn chưa hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Mình lý giải và hướng dẫn lại nha
#Hibernate #list