Hibernate多对一双向关联(Annotation配置)

转载地址:http://www.blogjava.net/sxyx2008/archive/2010/10/18/335462.html


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. class 1 );
            System.out.println(employee.getName());
            System.out.println(employee.getDepartment().getName());
        } 
catch  (HibernateException e) {
            e.printStackTrace();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值