Hibernate many-to-one equipment

Source: Internet
Author: User

There are two conditions for many-to-one equipment in hibernate:
One is the primary key ID of the primary table that the foreign key of the child table corresponds to.
The other is that the foreign key of the child table corresponds to the primary table's non-primary key field, which is usually the only field. (Common scenario is the order Main table with the order product schedule, the main table and the child table are the ID as the primary key, but between the two by OrderNo (order number) to relate)

According to these two conditions, a brief introduction to the number of the hibernate mapping file equipment.
In the first case, assuming that the main table is part of the table (department), the Keyword Mining tool table is the employee table (employee).
The domain class is as follows:
Java Code Preservation Code

Guoyi Number One http://www.tdhzp.com

Linyi Wholesale network http://www.shoudashou.com
Package Com.xigua.domain;


Public class Department {

/** 
* Primary Key ID
*/ 
private Long ID;

/** 
* Partial title
*/ 
private String departname;

Public Long getId () {
return ID;


Public void SetId (Long id) {
this.id = ID;


Public String Getdepartname () {
return departname;


Public void Setdepartname (String departname) {
this.departname = departname;




Java Code Preservation Code
Package Com.xigua.domain;

/** 
* Staff
* @author Administrator

*/ 
Public class Employee {

/** 
* Primary Key ID
*/ 
private Long ID;

/** 
* Employee Title
*/ 
private String EmpName;

/** 
* Part of the goal
*/ 
private Department Department;

Public Long getId () {
return ID;


Public void SetId (Long id) {
this.id = ID;


Public String Getempname () {
return empname;


Public void Setempname (String empname) {
this.empname = empname;


Public Department getdepartment () {
return department;


Public void Setdepartment (Department Department) {
this.department = Department;



the corresponding hibernate mapping files are as follows:
XML Code Preservation Code

"-//hibernate/hibernate Mapping DTD 3.0//en"
"D:\TOOLS\HIBERNATE\DTD\HIBERNATE-MAPPING-3.0.DTD" >















XML Code Preservation Code

"-//hibernate/hibernate Mapping DTD 3.0//en"
"D:\TOOLS\HIBERNATE\DTD\HIBERNATE-MAPPING-3.0.DTD" >














after the above-mentioned equipment, the employee and department have a lot of binding.
you can tap the following code to verify:
Java Code Preservation Code
Package com.xigua.test;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.xigua.domain.Department;
import Com.xigua.domain.Employee;
import Com.xigua.utils.HibernateUtil;

Public class Test1 {

Public static void Main (String args[]) {

save ();
query ();


Public static void Save () {
Session session = NULL;
Transaction tx = null;
try{
session = Hibernateutil.getsession ();
tx = Session.begintransaction ();

Department depart = new Department ();
depart.setdepartname ("Dev");

Employee EMP1 = new Employee ();
emp1.setempname ("xigua1");

//Cascade reserved part of employee
emp1.setdepartment (depart);



Session.save (depart);
Session.save (EMP1);

tx.commit ();
}catch (Exception e) {
if (tx! = null) {
Tx.rollback ();

throw e;
}finally {
if (session! = NULL) {
session.close ();




Public static void query () {
Session session = NULL;
try{
session = Hibernateutil.getsession ();

Employee EMP = (employee) session.get (Employee.class, 1L);
System.out.println (Emp.getempname ());

//Cascade check employee corresponding part
Department depart = Emp.getdepartment ();
System.out.println (Depart.getdepartname ());
}catch (Exception e) {
throw e;
}finally {
if (session! = NULL) {
session.close ();






If you want to bind a one-to-many relationship in the department target, then you need to modify the Department.java class with the corresponding hibernate mapping file.
a set mobilization target is added to the Department.java class, and a tag is also equipped in the corresponding hibernate file, as detailed in the following code.
Add the Department.java class after set-up target:
Java Code Preservation Code
Package Com.xigua.domain;

import Java.util.Set;


Public class Department {

/** 
* Primary Key ID
*/ 
private Long ID;

/** 
* Partial title
*/ 
private String departname;

/** 
* Set to mobilize staff information in the presence of this section
*/ 
Public setemps;

Public Long getId () {
return ID;


Public void SetId (Long id) {
this.id = ID;


Public String Getdepartname () {
return departname;


Public void Setdepartname (String departname) {
this.departname = departname;


Public Setgetemps () {
return emps;


Public void Setemps (setemps) {
this.emps = emps;



Hibernate many-to-one equipment

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.