Hibernate Getting Started: Association rules for many-to-one and a-to-many

Source: Internet
Author: User
Tags commit generator
Many-to-one rule: for example, employee and department, more than one employee corresponds to a department, called many to one; a one-to-many rule: such as department and employee, A department corresponds to multiple employee, called a one-to-many;


One -to-many rules:

public class Department {
	private int id;
	private String name;
	Private set<employee> Emps;	This is a one-to-many 
	getter setter ...
}



public class Employee {
	private int id;
	private String name;
	Getter Setter ...
}


Set in Department.hbm.xml:

<set name= "Emps" >
        <key column= "depart_id" ></key>	<!--Because collection properties cannot be stored in this table, you need to open another table. Therefore, you need to specify that the table that stores this collection is used to connect foreign keys to the ID property of the Department table--
        <!--depart_id Property
        associated with the ID of the employee table and the Department table. <one-to-many class= "Employee"/><!--Specify a one-to-many corresponding table-
</set>

After a one-to-many rule setting, there is no change in department, and the change is a depart_id attribute in the employee;
Many-to-one rule:

public class Department {
	private int id;
	private String name;
	Getter Setter ...
}


Package Org.xiazdong;

public class Employee {
	private int id;
	Private Department depart;	This is a many-to-one, referenced by the Department object department
	Private String name;
	Getter Setter ...
}

Settings in Employee.hbm.xml:

<many-to-one name= "Depart" column= "depart_id"/>	<!--settings are associated with a depart_id and department primary key in the employee-- >


Instance:


Employee.java

Package Org.xiazdong;

public class Employee {
	private int id;
	Private Department depart;	This is a many-to-one
	private String name;
	public int getId () {
		return ID;
	}
	public void setId (int id) {
		this.id = ID;
	}
	Public String GetName () {
		return name;
	}
	public void SetName (String name) {
		this.name = name;
	}
	Public Department Getdepart () {
		return depart;
	}
	public void Setdepart (Department depart) {
		This.depart = depart;
	}
}

Department.java


Package Org.xiazdong;

Import Java.util.Set;

public class Department {
	private int id;
	private String name;
	Private set<employee> Emps;	This is a one-to-many public
	 
	set<employee> getemps () {
		return emps;
	}
	public void Setemps (set<employee> emps) {
		this.emps = emps;
	}
	public int getId () {
		return ID;
	}
	public void setId (int id) {
		this.id = ID;
	}
	Public String GetName () {
		return name;
	}
	public void SetName (String name) {
		this.name = name;
	}
	
}

Employee.hbm.xml


<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
<!--Generated 2012-5-17 10:59:47 by Hibernate Tools 3.4.0.CR1--
< Hibernate-mapping package= "Org.xiazdong" >
	<class name= "Employee" >
		<id name= "id" >
			< Generator class= "native"/>
		</id>
		<property name= "name"/>
		<many-to-one name= "Depart" column= "depart_id"/>
	</class>


Department.hbm.xml


<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
<!--Generated 2012-5-17 10:59:47 by Hibernate Tools 3.4.0.CR1--
< Hibernate-mapping package= "Org.xiazdong" >
    <class name= "Department" >
        <id name= "id" >
            <generator class= "native"/>
        </id>
        <property name= "name"/>
        
        <set name= "Emps" >
        	<key column= "depart_id" ></key>	<!--Because collection properties cannot be stored in this table, you need to open another table. Therefore, you need to specify that the table that stores this collection is used to connect foreign keys to the ID property of the Department table-
        	<!--depart_id is the primary key for the employee table, and the ID of the department table is associated
        	with- <one-to-many class= "Employee"/><!--Specify a one-to-many corresponding table-
        </set>
    </class>
</ Hibernate-mapping>

The following table is executed:

Department


Employee



Test class:

Package test;
Import org.hibernate.Session;
Import org.hibernate.Transaction;
Import Org.hibernate.util.HibernateUtil;
Import org.xiazdong.Department;

Import Org.xiazdong.Employee;
		public class Many2onetest {public static void main (string[] args) {add ();
	Query ();
		} static void Query () {Session session = Hibernateutil.getsession ();
		Transaction tx = Session.begintransaction ();
		Department depart = (Department) session.get (Department.class, 6);
		Employee E = (employee) session.get (Employee.class, 8);
		System.out.println (Depart.getemps ());
		System.out.println (E.getdepart (). GetName ());
		Tx.commit ();
	Session.close ();
		} static void Add () {Department depart = new Department ();
		Depart.setname ("Depart");
		Employee e1 = new Employee ();
		E1.setname ("E1");
		E1.setdepart (depart);
		Employee E2 = new Employee ();
		E2.setname ("E2");
		
		E2.setdepart (depart);
		Session session = Hibernateutil.getsession ();
		Transaction tx = Session.begintransaction (); Session.save (DEPART);
		Session.save (E1);
		Session.save (E2);
		Tx.commit ();
	Session.close ();
 }
}






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.