Hibernate 一对多映射关联映射

Hibernate 一对多映射

 

  • java类

 

public class Department{
     private String departmentId;
     private String departmentName;
     private List<Employee> employee;
    public Department() {
    }
    public Department(String departmentId) {
        this.departmentId = departmentId;
    }
    public Department(String departmentId, String departmentName,List<Employee> personList) {
       this.departmentId = departmentId;
       this.departmentName = departmentName;
       this.employee=personList;
    }
   
    public String getDepartmentId() {
        return this.departmentId;
    }
    
    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }
    public String getDepartmentName() {
        return this.departmentName;
    }
    
    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }


	public List<Employee> getEmployee() {
		return employee;
	}


	public void setEmployee(List<Employee> employee) {
		this.employee = employee;
	}
}



public class Employee{
     private String id;
     private String name;
     private String sex;
     private Department department;
     private IdentityCard identityCard;

    public Employee() {
    }

	
    public Employee(String id) {
        this.id = id;
    }
    public Employee(String id, String name, String sex ){
       this.id = id;
       this.name = name;
       this.sex = sex;
    }
   
    public String getId() {
        return this.id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return this.sex;
    }
    
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Department getDepartment() {
        return this.department;
    }
    
    public void setDepartment(Department department) {
        this.department = department;
    }


	public IdentityCard getIdentityCard() {
		return identityCard;
	}


	public void setIdentityCard(IdentityCard identityCard) {
		this.identityCard = identityCard;
	}




}

 

  • 映射文件
<hibernate-mapping package="com.woo.demo.hibernate.domain">
    <class name="Employee" table="EMPLOYEE">
        <id name="id" type="java.lang.String">
            <column name="ID" />
            <generator class="uuid"/>
        </id>
        <property name="name" type="text">
            <column name="NAME" />
        </property>
        <property name="sex" type="string">
            <column name="SEX" />
        </property>
        <many-to-one name="department" class="Department" >
            <column name="DEPARTMENT_ID"/>
        </many-to-one>
    </class>
</hibernate-mapping>


<hibernate-mapping package="com.woo.demo.hibernate.domain">
    <class name="Department" table="DEPARTMENT">
        <id name="departmentId" type="java.lang.String">
            <column name="DEPARTMENT_ID" />
            <generator class="uuid" />
        </id>
        <property name="departmentName" type="java.lang.String">
            <column name="DEPARTMENT_NAME" />
        </property>        
        <bag name="employee" cascade="save-update">
        	<key column="DEPARTMENT_ID"/>
        	<one-to-many class="Employee"/>
        </bag>
    </class>
</hibernate-mapping>

 

  • 关系数据库结构



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值