hibernate配置的一些总结

many-to-one
<!--关联映射的本质:-->
	<!--* 将关联关系映射到数据库,所谓的关联关系是对象模型在内存中的一个或多个引用-->

<!--<many-to-one>会在多的一端加入一个外键,指向一的一端,这个外键是由<many-to-one>中的column属性定义的,如果忽略了这个属性那么默认的外键与实体的属性一致<many-to-one>标签的定义示例:-->
	<!-- * <many-to-one name="group" column="groupid"/>-->

<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.User" table="t_user">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<many-to-one name="group" column="groudid" cascade="save-update"></many-to-one>
	</class>
</hibernate-mapping>

<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.Group" table="t_group">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>		
	</class>
</hibernate-mapping>


one-to-one主键关连 单向
<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.Person" table="t_Person">
<!-- person的主键来源idCard,也就是共享idCard的主键 -->
		<id name="id">
			<generator class="foreign">
				<param name="property">idCard</param>
			</generator>
		</id>
		<property name="name"/>
<!-- one-to-one标签的含义,指示hibernate怎么加载它的关联对象,默认根据主键加载,constrained="true",表明当前主键上存在一个约束,person的主键作为外键参照了idCard -->	
		<one-to-one name="idCard" constrained="true"/>	
	</class>
</hibernate-mapping>

<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.IdCard" table="t_IdCard">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="num"/>		
	</class>
</hibernate-mapping>


one-to-one 主键关连 双向
<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.Person" table="t_Person">
		<id name="id">
			<generator class="foreign">
				<param name="property">idCard</param>
			</generator>
		</id>
		<property name="name"/>	
		<one-to-one name="idCard" constrained="true"/>	
	</class>
</hibernate-mapping>

<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.IdCard" table="t_IdCard">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="num"/>	
		<one-to-one name="person"/>	
	</class>
</hibernate-mapping>


one-to-one 唯一外键关连 单向 就是多对一
<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.Person" table="t_Person">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>	
<!--可以采用<many-to-one>标签,指定多的一端的unique=true,这样就限制了多的一端的多重性为一通过这种手段映射一对一唯一外键关联-->
		<many-to-one name="idCard" unique="true" cascade="all"/>	
	</class>
</hibernate-mapping>

<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.IdCard" table="t_IdCard">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="num"/>		
	</class>
</hibernate-mapping>


one-to-one 唯一外键 双向
<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.Person" table="t_Person">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>	
		<many-to-one name="idCard" unique="true" cascade="all"/>	
	</class>
</hibernate-mapping>
<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.IdCard" table="t_IdCard">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="num"/>	
		<one-to-one name="person" property-ref="idCard"/>	
	</class>
</hibernate-mapping>


one-to-many 单向
<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.Classes" table="t_classes">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<set name="students" cascade="all">
			<key column="classesid"/>
			<!-- 有缺点 -->
<!--多对一关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是多指向一-->
<!--一对多关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是一指向多-->
<!--* 如果将t_student表里的classesid字段设置为非空,则无法保存-->
<!--* 因为不是在student这一端维护关系,所以student不知道是哪个班的,-->
<!--所以需要发出多余的update语句来更新关系-->
			<one-to-many class="com.langhua.hibernate.pojo.Students"/>
		</set>
	</class>
</hibernate-mapping>

<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.Students" table="t_students">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
	</class>
</hibernate-mapping>


one-to-many 双向
<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.Classes" table="t_classes">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<set name="students" inverse="true" cascade="all">
			<key column="classesid"/>
			<!-- 有缺点 -->
			<one-to-many class="com.langhua.hibernate.pojo.Students"/>
		</set>
	</class>
</hibernate-mapping>

<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.Students" table="t_students">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<many-to-one name="classes" column="classesid"/>
	</class>
</hibernate-mapping>


many-to-many 双向
<!--table属性值必须和单向关联中的table属性值一致-->
<!--<key>中column属性值要与单向关联中的<many-to-many>标签中的column属性值一致-->
<!--在<many-to-many>中的column属性值要与单向关联中<key>标签的column属性值一致-->
<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.User" table="t_user">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<set name="roleSet" table="t_user_role">
			<key column="userId"/>
			<many-to-many class="com.langhua.hibernate.pojo.Role" column="roleId"/>
		</set>
	</class>
</hibernate-mapping>

<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.Role" table="t_role">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<set name="userSet" table="t_user_role" inverse="true">
			<key column="roleId"/>
			<many-to-many class="com.langhua.hibernate.pojo.User" column="userId"/>
		</set>
	</class>
</hibernate-mapping>


一对多双向关联映射
* 在一一端的集合上使用<key>,在对方表中加入一个外键指向一一端
* 在多一端采用<many-to-one>

注意:<key>标签指定的外键字段必须和<many-to-one>指定的外键字段一致,否则引用字段的错误

如果在”一“一端维护一对多关联关系,hibernate会发出多余的udpate语句,所以我们一般在多
的一端来维护关联关系

关于inverse属性:
inverse主要用在一对多和多对多双向关联上,inverse可以被设置到集合标签<set>上,
默认inverse为false,所以我们可以从”一“一端和”多“一端维护关联关系,
如果设置成inverse为true,则我们只能从多一端来维护关联关系

注意:inverse属性,只影响数据的存储,也就是持久化

inverse和cascade
* inverse是关联关系的控制方向
* cascade操作上的连锁反应

复合主键

public class FiscalYearPeriod {
	
	private FiscalYearPeriodPK fiscalYearPeriodPK;
	
	//开始日期
	private Date beginDate;
	
	//结束日期
	private Date endDate;
	
	//状态
	private String periodSts;
}
public class FiscalYearPeriodPK implements Serializable {

	//核算年
	private int fiscalYear;
	
	//核算月
	private int fiscalPeriod;

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + fiscalPeriod;
		result = prime * result + fiscalYear;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final FiscalYearPeriodPK other = (FiscalYearPeriodPK) obj;
		if (fiscalPeriod != other.fiscalPeriod)
			return false;
		if (fiscalYear != other.fiscalYear)
			return false;
		return true;
	}
	
}



]
<hibernate-mapping>
	<class name="com.bjsxt.hibernate.FiscalYearPeriod" table="t_fiscal_year_period">
		<composite-id name="fiscalYearPeriodPK">
			<key-property name="fiscalYear"/>
			<key-property name="fiscalPeriod"/>	
		</composite-id>
		<property name="beginDate"/>
		<property name="endDate"/>
		<property name="periodSts"/>
	</class>
</hibernate-mapping>

集合
public class CollectionMapping implements Serializable {
	private int id;
	private String name;
	private Set setValue;
	private List listValue;
	private String[] arrayValue;
	private Map mapValue;
}

<hibernate-mapping>
	<class name="com.langhua.hibernate.pojo.CollectionMapping" table="t_CollectionMapping">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		
		<set name="setValue" table="t_setValue">
			<key column="set_id"/>
			<!-- 
			<composite-element class="">
				这个是配置对象
			</composite-element>
			 -->
			 <element type="string" column="set_value"/>
		</set>
		
		<list name="listValue" table="t_listValue">
			<key column="list_id"/>
			<list-index column="list_index"/>
			<element type="string" column="list_value"/>
		</list>
		
		<array name="arrayValue" table="t_arrayValue">
			<key column="array_id"/>
			<list-index column="list_index"/>
			<element type="string" column="array_value"/>
		</array>
		
		<map name="mapValue" table="t_mapValue">
			<key column="map_id"/>
			<map-key type="string" column="map_key"/>
			<element type="string" column="map_value"/>
		</map>
	</class>
</hibernate-mapping>

你可能感兴趣的:(Hibernate,xml)