spring many-to-one 关联查询

多的一方 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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.ssh2.bean.User" table="[User]" schema="dbo" catalog="plusoft_test">
        <id name="id" type="java.lang.Long">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="firstName" type="java.lang.String">
            <column name="FirstName" length="50" />
        </property>
        <property name="lastName" type="java.lang.String">
            <column name="LastName" length="50" />
        </property>
        <property name="age" type="java.lang.Integer">
            <column name="Age" />
        </property>
        <many-to-one name="ownClass" column="Class_Id" class="com.ssh2.bean.OwnClass"  cascade="all"  lazy="false"/>  
    </class>
</hibernate-mapping>

一的 一方 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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.ssh2.bean.OwnClass" table="OwnClass" schema="dbo" catalog="plusoft_test">
        <id name="id" type="java.lang.Long">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="className" type="java.lang.String">
            <column name="ClassName" length="250" />
        </property>
        <property name="description" type="java.lang.String">
            <column name="Description" length="1024" />
        </property>
        <!-- <set name="Users" inverse="true" cascade="all">
           <key column="Class_Id" ></key>                
           <one-to-many class="com.ssh2.bean.User"/>            
        </set>-->
    </class>
</hibernate-mapping>

User.java

package com.ssh2.bean;

/**
 * User entity. @author MyEclipse Persistence Tools
 */

public class User implements java.io.Serializable {

	private Long id;
	private String firstName;
	private String lastName;
	private Integer age;
	
	/**
	 * 人员与所属班级的关联
	 */
	private OwnClass ownClass;

	public User() {
	}

	public OwnClass getOwnClass() {
		return ownClass;
	}

	public void setOwnClass(OwnClass ownClass) {
		this.ownClass = ownClass;
	}

	/** full constructor */
	public User(String firstName, String lastName, Integer age) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
	}

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFirstName() {
		return this.firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return this.lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Integer getAge() {
		return this.age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}
}

相关推荐