Configure the Hibernate associated ing file,

Source: Internet
Author: User

Configure the Hibernate associated ing file,

I. Multi-to-one-way Association

First, we must create two entity classes.

Example: Dept class

public class Dept {private Integer deptNo;private String  dName;private String loc;private  Set<Employee> emps=new HashSet<Employee>();

Employee class

public class Employee {     private Integer empno;     private String ename;          private  Dept dept;

 

Now we can configure it in the configuration file.

Dept. hbm. xml

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <Set name = "emps" cascade = "save-update" inverse = "true"> <key column = "deptNo"> </key> <! -- One-to-many --> <one-to-learn class = "Employee"/> </set></Class> 

Employee. hbm. xml

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <Role-to-one name = "dept" column = "deptNo" class = "Dept"> </role-to-one><Set name = "projects" inverse = "true" table = "proemp"> <key column = "rempNo"> </key> <example-to-example class = "Project "column =" rproNo "> </role-to-sequence> </set> </class> 

By now, the one-to-many and one-to-one association configuration has been completed !!

Let's take a look at the test class.

// Add private static void addDeptAndEmployee () {Session currentSession = HibernateUtil. currentSession (); currentSession. beginTransaction (); Dept dept = new Dept (); // dept. setDeptNo (deptNo) dept. setdName ("QC Department 22"); dept. setLoc ("Beijing 22"); Employee emp = new Employee (); emp. setEname ("dragon ");Emp. setDept (dept );// Establish the association between dept and employee objectCurrentSession. save (emp); currentSession. save (dept );CurrentSession. getTransaction (). commit (); HibernateUtil. closeSessio (); System. out. println ("add OK ");}

 

// Cascade save cascade: cascade private static void addDeptAndEmployeeTocascade () {Session currentSession = HibernateUtil. currentSession (); currentSession. beginTransaction (); Dept dept = new Dept (); // dept. setDeptNo (deptNo) dept. setdName ("QC Department 22"); dept. setLoc ("Beijing 22"); Employee emp = new Employee (); emp. setEname ("dragon"); // establishes a one-to-multiple bidirectional relationship between the Dept object and the Employee objectEmp. setDept (dept); dept. getEmps (). add (emp );//CurrentSession. save (dept );CurrentSession. getTransaction (). commit (); HibernateUtil. closeSessio (); System. out. println ("add OK ");}

 

Now let's look at the many-to-many Association.

We have created entity classes.

For example:

Employee

Public class Employee {private Integer empno; private String ename; private Dept dept;Private Set <Project> projects = new HashSet <Project> (0); // attributes of the Set class employee

Project

Public class Project {private Integer proNo; // Project No. private String proName; // Project namePrivate Set <Employee> employees = new HashSet <Employee> (0); // attributes of the Set class employee

 

Note: I also created another table proemp in the Oracle tool, that isConnection tableYou can see the foreign key relationship.

create table proemp(rproNo number(6),rempNo number(6))alter table proemp add constraints FK_rproNo  FOREIGN KEY(rproNo) REFERENCES  Project(prono)alter table proemp add constraints FK_rempNo  FOREIGN KEY(rempNo) REFERENCES  employee(empNo)

 

Then configure the ing file.

One-way, many-to-many Association

Employee. hbm. xml

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <Set name = "projects" inverse = "true" table = "proemp"> <key column = "rempNo"> </key> <example-to-example class = "Project "column =" rproNo "> </role-to-sequence> </set></Class> 

Bidirectional, many-to-many

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <Set name = "employees" table = "proemp" cascade = "save-update"> <key column = "rproNo"> </key> <allow-to-learn class = "Employee" column = "rempNo"> </Employee-to-role> </set></Class> 

Let's take a look at the test class.

Private static void addProToEmpAndEmpToPro () {Session session = HibernateUtil. currentSession (); session. beginTransaction (); Employee emp = new Employee (); Employee emp2 = new Employee (); Project pro = new Project (); Project pro2 = new Project (); emp. setEname ("Li Si"); emp2.setEname ("Zhang San"); pro. setProName ("Project 1"); pro2.setProName ("Project 2 ");Pro. getEmployees (). add (emp); pro. getEmployees (). add (emp2); emp. getProjects (). add (pro); emp. getProjects (). add (pro2); pro2.getEmployees (). add (emp); emp. getProjects (). add (pro2); session. save (pro); session. save (pro2 );HibernateUtil. closeSessio ();}

 

// Save the Project object and save the Employee object private static void addProToEmp () {Session session = HibernateUtil. currentSession (); session. beginTransaction (); Employee emp = new Employee (); Employee emp2 = new Employee (); Project pro = new Project (); Project pro2 = new Project (); emp. setEname ("Li Si"); emp2.setEname ("Zhang San"); pro. setProName ("Project 1"); pro2.setProName ("Project 2 ");Pro. getEmployees (). add (emp); pro. getEmployees (). add (emp2); pro2.getEmployees (). add (emp); session. save (pro); session. save (pro2 );HibernateUtil. closeSessio ();}

Now, you can answer questions online !!!!!!!!!!!!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.