hibernate映射关系

hibernate映射关系

  • 目录
    • 说明
    • 实体类User.java
    • 实体类Department.java
    • 实体类Course.java
    • 配置文件User.hbm.xml
    • 配置文件Department.hbm.xml
    • 配置文件Course.hbm.xml
    • 总结

目录

说明

一对多,多对多

  1. User.java和Course.java是多对多
  2. User.java和Department.java是多对一

实体类User.java

代码片.

package com.entity.manytomany;

import java.util.Set;

public class User implements java.io.Serializable {

	private Long usrId;
	//一方的引用
	private Department department;
	private String usrName;
	private String usrPassword;
	private String usrDesc;
	private String sex;
	private Integer age;
	//多方的集合
	private Set<Course> courses;
	
	
	public Long getUsrId() {
		return usrId;
	}
	public void setUsrId(Long usrId) {
		this.usrId = usrId;
	}
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	public String getUsrName() {
		return usrName;
	}
	public void setUsrName(String usrName) {
		this.usrName = usrName;
	}
	public String getUsrPassword() {
		return usrPassword;
	}
	public void setUsrPassword(String usrPassword) {
		this.usrPassword = usrPassword;
	}
	public String getUsrDesc() {
		return usrDesc;
	}
	public void setUsrDesc(String usrDesc) {
		this.usrDesc = usrDesc;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Set<Course> getCourses() {
		return courses;
	}
	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}
	
	

}

实体类Department.java

代码片.

package com.entity.manytomany;

import java.util.Set;

public class Department implements java.io.Serializable {

	// Fields

	private Integer depId;
	private String depName;
	//多方的集合
	private Set<User> users;
	
	public Integer getDepId() {
		return depId;
	}
	public void setDepId(Integer depId) {
		this.depId = depId;
	}
	public String getDepName() {
		return depName;
	}
	public void setDepName(String depName) {
		this.depName = depName;
	}
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	

}

实体类Course.java

代码片.

package com.entity.manytomany;

import java.util.Set;


public class Course implements java.io.Serializable {

	private Integer id;
	private String cname;
	//多方的集合
	private Set<User> users;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}



}

配置文件User.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>
    <class name="com.entity.manytomany.User" table="user" catalog="testdb">

		<!-- 组合主键一 
       <component name="组件名" class="组件类的全名">
       		<property name="属性名1" column="字段名1"/>
       		<property name="属性名2" column="字段名2"/>
       		<property name="属性名3" column="字段名3"/> 
       </component>
    	-->

	    <id name="usrId" type="java.lang.Long">
            <column name="usrId" />
            <generator class="identity" />
        </id>
        
        <!-- 多对一 -->
        <many-to-one name="department" class="com.entity.manytomany.Department"  >
        	 <!-- 对方的主键名 -->
            <column name="depId" not-null="true" />
        </many-to-one>
        
        <property name="usrName" type="java.lang.String">
            <column name="usr_name" length="50" not-null="true" />
        </property>
        <property name="usrPassword" type="java.lang.String">
            <column name="usr_password" length="50" not-null="true" />
        </property>
        <property name="usrDesc" type="java.lang.String">
            <column name="usr_desc" length="50" not-null="true" />
        </property>
        <property name="sex" type="java.lang.String">
            <column name="sex" length="2"  />
        </property>
        <property name="age" type="java.lang.Integer">
            <column name="age"  />
        </property>
        
      
        <!-- 多对多 ,创建关系表 usr_course-->
        <set name="courses" table="usr_course" catalog="testdb" >
        	<!-- 外键 -->
            <key>
                <column name="usrId" not-null="true" />
            </key>
            
            <!-- 多对多-->
            <many-to-many entity-name="com.entity.manytomany.Course">
            	<!-- Course的外键 (可以重命名,courseid)-->
                <column name="courseid" not-null="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>

配置文件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>
    <class name="com.entity.manytomany.Department" table="department" catalog="testdb">
        <id name="depId" type="java.lang.Integer">
            <column name="depId" />
            <generator class="identity" />
        </id>
        <property name="depName" type="java.lang.String">
            <column name="depName" length="45" not-null="true" />
        </property>
        
        <!-- 一对多 ,让出控制权:inverse="true" -->
        <set name="users" inverse="true" >
        
            <!-- 主键名 -->
            <key column="depId" not-null="true" />
            <one-to-many class="com.entity.manytomany.User" />
        </set>
    </class>
</hibernate-mapping>

配置文件Course.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>
    <class name="com.entity.manytomany.Course" table="course" catalog="testdb">
        <id name="id" type="java.lang.Integer">
            <column name="courseid" />
            <generator class="native"></generator>
        </id>
        <property name="cname" type="java.lang.String">
            <column name="cname" length="45" not-null="true" />
        </property>
        
        <!-- 多对多 ,创建关系表 usr_course,inverse="true":控制器让给对方表-->
        <set name="users" inverse="true" table="usr_course" catalog="testdb">
        	<!-- 外键 -->
            <key>
                <column name="courseid" not-null="true" />
            </key>
            
            <!-- 多对多-->
            <many-to-many entity-name="com.entity.manytomany.User">
            	<!-- User的外键 -->
                <column name="usrid" not-null="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>

总结

	1. 在一中定义多方引用: Object.
	2. 在多方定义一方的集: 合Set < >.
	3. 一方交出控制权:< set  inverse="true"  >.
**引用包是最大的困难(一步一步来)**

你可能感兴趣的:(hibernate)