`
zc_888
  • 浏览: 19760 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
文章分类
社区版块
存档分类
最新评论

Hibernate级联实践之三(many-to-many)

 
阅读更多

一。mapping

使用三个表,分别为TBL_OSU_PRODUCT_I、TBL_OSU_BUSINESSFUNCTION_I和TBL_OSU_PRODUCTBUSINESS_C,1和2为多对多关系,3为其中间表

1.TBL_OSU_PRODUCT_I

<?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.netqin.function.osuproduct.product.model.OsuProduct" table="TBL_OSU_PRODUCT_I" schema="OSU_WORK">
<id name="id" type="long">
<column name="PRDU_ID" precision="10" scale="0" />
<generator class="sequence">
<param name="sequence">SEQ_OSU_PRODUCT_I</param>
</generator>
</id>
<property name="prduName" type="string">
<column name="PRDU_NAME" length="200" not-null="true" />
</property>
<property name="prduDescription" type="string">
<column name="PRDU_DESCRIPTION" length="200" />
</property>

<set name="business" table="TBL_OSU_PRODUCTBUSINESS_C" lazy="false" inverse="false" cascade="save-update" schema="OSU_WORK">
<key column="PRBU_PRDUID_FK"/>
<many-to-many class="com.netqin.function.osuproduct.businessfunction.model.BusinessFunction" column="PRBU_BUFUID_FK"/>
</set>

</class>
</hibernate-mapping>

说明:只有1中做了关联配置。lazy="false" :加载1时级联加载2,方便页面使用(看需要使用)。inverse="false":控制权由1负责。

cascade="save-update" :级联关系为新增和修改,不能加上delete,否则会一并删除2中的记录。

2.TBL_OSU_BUSINESSFUNCTION_I

<?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.netqin.function.osuproduct.businessfunction.model.BusinessFunction" table="TBL_OSU_BUSINESSFUNCTION_I" schema="USERINFOV2">
<id name="bufuId" type="java.lang.String">
<column name="BUFU_ID" length="10" />
<generator class="uuid.hex" />
</id>
<property name="bufuBusiidFk" type="java.lang.String">
<column name="BUFU_BUSIID_FK" length="10" not-null="true" />
</property>
<property name="bufuName" type="java.lang.String">
<column name="BUFU_NAME" length="64" not-null="true" />
</property>
<property name="bufuType" type="java.lang.Long">
<column name="BUFU_TYPE" precision="4" scale="0" not-null="true" />
</property>

</class>
</hibernate-mapping>

说明:没有做关联


3.TBL_OSU_PRODUCTBUSINESS_C

没有映射文件,仅有两个字段,代表两个表的主键,分别为PRBU_PRDUID_FK和PRBU_BUFUID_FK,前者为1的主键,后者为2的主键

二。操作方法

1.新增

String[] businesses= request.getParameterValues("businesses");

if (businesses != null&& businesses.length > 0) {
for (int i = 0; i < businesses.length; i++) {
BusinessFunction bf = (BusinessFunction)dao.findById(BusinessFunction.class, businesses[i]);//根据主键从数据库中查询出另一方的对象
object.getBusiness().add(bf);//关联对象
}
}

dao.save(object);

2.修改

OsuProduct object_tem = this.findById(object.getId());//从数据库中查询出主表对象

object_tem.getBusiness().removeAll(object_tem.getBusiness());//清空原有级联对象

copyProject(object_tem, object);//其余属性复制,同many-to-one

String[] businesses= request.getParameterValues("businesses");

if (businesses != null&& businesses.length > 0) {
for (int i = 0; i < businesses.length; i++) {
BusinessFunction bf = (BusinessFunction)dao.findById(BusinessFunction.class, businesses[i]);
object_tem.getBusiness().add(bf);
}
}

dao.update(object_tem);

3.删除

删除主表即可

dao.delete(object);

分享到:
评论

相关推荐

    hibernate many-to-many级联保存,级联更新,级联删除

    hibernate many-to-many级联保存,级联更新,级联删除

    hibernate many-to-one(多对一)及 cascade(级联).doc

    深入理解hibernate many-to-one(多对一)及 cascade(级联).

    Hibernate many-to-many

    两套hibernate级联例子,jar包已经包含在里面了,运行代码也写好了,数据库直接会自动生成表的,就差你运行了,你懂的

    hibernate3.2中文文档(chm格式)

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合...

    HibernateAPI中文版.chm

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合...

    Hibernate+中文文档

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合...

    hibernate总结

    i. Many-to-one 1. false 2. proxy 3. no-proxy ii. set 一对多 1. true  2. false  3. extra 根据对set容器的不同,可以产生高效的sql访问数据库 2. 批量检索:batch-size=3 a) 可以使用批量检索: b) 在内存...

    最全Hibernate 参考文档

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted ...

    Hibernate中文详细学习文档

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合...

    Hibernate 中文 html 帮助文档

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted ...

    hibernate 体系结构与配置 参考文档(html)

    值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted ...

    Hibernate教程

    7.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted...

    Hibernate3+中文参考文档

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted ...

    hibernate3.04中文文档.chm

    7.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合...

    hibernate 框架详解

    值集合于多对多关联(Collections of values and many-to-many associations) 7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted ...

    hibernate 教程

    值集合和多对多关联(Collections of Values and Many-To-Many Associations) 6.4. 一对多关联(One-To-Many Associations) 6.5. 延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted ...

    NHibernate中文文档

    值集合和多对多关联(Collections of Values and Many-To-Many Associations) 14 一对多关联(One-To-Many Associations) 14 延迟初始化(延迟加载)(Lazy Initialization) 14 集合排序(Sorted Collections) 14 使用 ...

    Hibernate参考文档

    6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted ...

    hibernate

    值集合和多对多关联(Collections of Values and Many-To-Many Associations) 6.4. 一对多关联(One-To-Many Associations) 6.5. 延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted ...

Global site tag (gtag.js) - Google Analytics