温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处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;

public class Department {
    
    
private int id;
    
private String name;
    
private Set<Employee> sets;
    
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 Set<Employee> getSets() {
        
return sets;
    }
    
public void setSets(Set<Employee> sets) {
        
this.sets = sets;
    }
    
}
Employee.java
package com.many2one.bean;


public class Employee {
    
    
private int id;
    
private String name;
    
private Department department;
    
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 getDepartment() {
        
return department;
    }
    
public void setDepartment(Department department) {
        
this.department = department;
    }
    
    
}
映射文件
Department.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
    
<hibernate-mapping package="com.many2one.bean">
        
<class name="Department" table="department" catalog="Hibernate_Many2One">
            
<id name="id" column="id">
                
<generator class="native"/>
            
</id>
            
<property name="name" column="name"/>
            
        
</class>
    
</hibernate-mapping>
Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
    
<hibernate-mapping package="com.many2one.bean">
        
<class name="Employee" table="employee" catalog="Hibernate_Many2One">
            
<id name="id" column="id">
                
<generator class="native"/>
            
</id>
            
<property name="name" column="name"/>
            
<many-to-one name="department" column="deptid" cascade="all" />
        
</class>
    
</hibernate-mapping>
导出的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:40 雪山飞鹄 阅读(2241) 评论(0)  编辑  收藏 所属分类: Hibernate

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


网站导航: