温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

Department.java
package com.many2one.bean;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name
="department",catalog="Hibernate_Many2One")
public class Department {
    
    
private int id;
    
private String name;
    
private Set<Employee> sets;
    @Id
    @GeneratedValue(strategy
=GenerationType.AUTO)
    @Column(name
="id")
    
public int getId() {
        
return id;
    }
    
public void setId(int id) {
        
this.id = id;
    }
    @Column(name
="name")
    
public String getName() {
        
return name;
    }
    
public void setName(String name) {
        
this.name = name;
    }
    @OneToMany(mappedBy
="department")
    
public Set<Employee> getSets() {
        
return sets;
    }
    
public void setSets(Set<Employee> sets) {
        
this.sets = sets;
    }
    
}
Employee.java
package com.many2one.bean;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name
="employee",catalog="Hibernate_Many2One")
public class Employee {
    
    
private int id;
    
private String name;
    
private Department department;
    @Id
    @Column(name
="id")
    @GeneratedValue(strategy
=GenerationType.AUTO)
    
public int getId() {
        
return id;
    }
    
public void setId(int id) {
        
this.id = id;
    }
    @Column(name
="name")
    
public String getName() {
        
return name;
    }
    
public void setName(String name) {
        
this.name = name;
    }
    @ManyToOne(cascade
=CascadeType.ALL)
    @JoinColumn(name
="deptid")
    
public Department getDepartment() {
        
return department;
    }
    
public void setDepartment(Department department) {
        
this.department = department;
    }
    
    
}
导出的sql
create table Hibernate_Many2One.department (
        id 
integer not null auto_increment,
        name 
varchar(255),
        
primary key (id)
    )

    
create table Hibernate_Many2One.employee (
        id 
integer not null auto_increment,
        name 
varchar(255),
        deptid 
integer,
        
primary key (id)
    )

    
alter table Hibernate_Many2One.employee 
        
add index FK4722E6AE3829E242 (deptid), 
        
add constraint FK4722E6AE3829E242 
        
foreign key (deptid) 
        
references Hibernate_Many2One.department (id)

测试代码
@Test
    
public void insert(){
        Session session
=HibernateSessionFactory.getSession();
        Transaction transaction
=session.beginTransaction();
        
try {
            transaction.begin();
            Department department
=new Department();
            department.setName(
"软件测试部");
            Employee employee
=new Employee();
            employee.setName(
"张三");
            employee.setDepartment(department);
            session.save(employee);
            transaction.commit();
        } 
catch (HibernateException e) {
            e.printStackTrace();
        }
    }
    
    
    @Test
    
public void select(){
        Session session
=HibernateSessionFactory.getSession();
        
try {
            Employee employee
=(Employee) session.get(Employee.class1);
            System.out.println(employee.getName());
            System.out.println(employee.getDepartment().getName());
        } 
catch (HibernateException e) {
            e.printStackTrace();
        }
    }
posted on 2010-10-18 15:46 雪山飞鹄 阅读(1462) 评论(1)  编辑  收藏 所属分类: Hibernate

Feedback

# re: Hibernate多对一双向关联(Annotation配置) 2013-07-18 16:23 木叶下
问一下,部门department应该是不能重复的字段,如果设置成unique,就保存不成功,请问怎么处理?  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: