今天在hibernate-hbm.xml里面做了调试发现casecade 和 inverse 的用法所在:
1.
public class FlowForm { private int id; private String template; private Set fields; public Set getFields() { return fields; } public void setFields(Set fields) { this.fields = fields; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTemplate() { return template; } public void setTemplate(String template) { this.template = template; } |
public class FormField { private int id; /** * 表单域类型 */ private FieldType fieldType; /** *表单输域入类型 */ private FieldInput fieldInput; /** * 表单域名称 */ private String fieldName; /** * 表单域标签 */ private String fieldLabel; /** * 表单 * @return */ private FlowForm flowForm; /** * 表单域items * @return */ private List<FieldItem> items; public List<FieldItem> getItems() { return items; } public void setItems(List<FieldItem> items) { this.items = items; } public FieldInput getFieldInput() { return fieldInput; } public void setFieldInput(FieldInput fieldInput) { this.fieldInput = fieldInput; } public FlowForm getFlowForm() { return flowForm; } public void setFlowForm(FlowForm flowForm) { this.flowForm = flowForm; } public String getFieldLabel() { return fieldLabel; } public void setFieldLabel(String fieldLabel) { this.fieldLabel = fieldLabel; } public String getFieldName() { return fieldName; } public void setFieldName(String fieldName) { this.fieldName = fieldName; } public FieldType getFieldType() { return fieldType; } public void setFieldType(FieldType fieldType) { this.fieldType = fieldType; } public int getId() { return id; } public void setId(int id) { this.id = id; } |
测试:
Session session = null; session =HibernateUtils.getSession(); session.beginTransaction(); FlowForm flowForm = new FlowForm(); flowForm.setTemplate("楼中阁"); FormField field1 = new FormField(); field1.setFieldLabel("姓名:回钦波"); field1.setFieldName("name"); FormField field2 = new FormField(); field2.setFieldLabel("姓名2::回钦波"); field2.setFieldName("name2"); FormField field3 = new FormField(); field3.setFieldLabel("姓名3::回钦波"); field3.setFieldName("name3"); FormField field4 = new FormField(); field4.setFieldLabel("姓名4::回钦波"); field4.setFieldName("name4"); Set<FormField> set = new HashSet<FormField>(); set.add(field1); set.add(field2); set.add(field3); set.add(field4); flowForm.setFields(set); session.save(flowForm); session.getTransaction().commit(); if(session.getTransaction().isActive()){ session.getTransaction().wasRolledBack(); } HibernateUtils.closeSession(session); |
配置文件:
1.
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class table="T_FormField" name="com.nqyw.oa.model.FormField"> <id name="id"> <generator class="native"/> </id> <property name="fieldName"/> <property name="fieldLabel"></property> <many-to-one name="fieldType"></many-to-one> <many-to-one name="fieldInput"></many-to-one> <many-to-one name="flowForm"></many-to-one> <list table="T_FieldItem" name="items"> <key column="formfieldId"></key> <list-index column="itemIndex"></list-index> <composite-element class="com.nqyw.oa.model.FieldItem"> <property name="label"></property> <property name="value"></property> </composite-element> </list> </class> </hibernate-mapping>
|
2.
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class table="T_FlowForm" name="com.nqyw.oa.model.FlowForm"> <id name="id"> <generator class="native"/> </id> <property name="template"/> <set inverse="true" cascade="save-update" name="fields"> <key column="flowformId"></key> <one-to-many class="com.nqyw.oa.model.FormField"/> </set> </class> </hibernate-mapping>
|
如果有:cascade="save-update" 和 inverse="true" ,表示我父(FlowForm.java)爸爸要负责维护子的关系,
SQL语句是:
Hibernate: insert into T_FormField (fieldName, fieldLabel, fieldType, fieldInput, flowForm) values (?, ?, ?, ?, ?)
Hibernate: insert into T_FormField (fieldName, fieldLabel, fieldType, fieldInput, flowForm) values (?, ?, ?, ?, ?)
Hibernate: insert into T_FormField (fieldName, fieldLabel, fieldType, fieldInput, flowForm) values (?, ?, ?, ?, ?)
Hibernate: insert into T_FormField (fieldName, fieldLabel, fieldType, fieldInput, flowForm) values (?, ?, ?, ?, ?)
如果没有cascade="save-update",的话,保存父(FlowForm.java)时 子类(FormField.java)是不能被保存的。
------------------------------------------------------
如果有 :cascade="save-update" 而没有 inverse="true" ,表示我父(FlowForm.java)爸爸要负责维护子的关系,
SQL语句是:
Hibernate: insert into T_FlowForm (template) values (?)
Hibernate: insert into T_FormField (fieldName, fieldLabel, fieldType, fieldInput, flowForm) values (?, ?, ?, ?, ?)
Hibernate: insert into T_FormField (fieldName, fieldLabel, fieldType, fieldInput, flowForm) values (?, ?, ?, ?, ?)
Hibernate: insert into T_FormField (fieldName, fieldLabel, fieldType, fieldInput, flowForm) values (?, ?, ?, ?, ?)
Hibernate: insert into T_FormField (fieldName, fieldLabel, fieldType, fieldInput, flowForm) values (?, ?, ?, ?, ?)
Hibernate: update T_FormField set flowformId=? where id=?
Hibernate: update T_FormField set flowformId=? where id=?
Hibernate: update T_FormField set flowformId=? where id=?
Hibernate: update T_FormField set flowformId=? where id=?
多了四条是因为父类这边在添加完父子下,还要更新管理下子类。所以最好加上 inverse="true"。这样会提高效率
如果没有cascade="save-update",的话,保存父(FlowForm.java)时 子类(FormField.java)是不能被保存的。
- 大小: 50.1 KB
分享到:
相关推荐
hibernate inverse和cascade的详细讲解,相当详细全面的对inverse和cascade区别和用法的讲解
inverse Hibernate_级联关系说明_-_关于cascade和inverse的用法
cascade-MVSNet——CVPR-202(源码、原文、译文) cascade-MVSNet——CVPR-202(源码、原文、译文) cascade-MVSNet——CVPR-202(源码、原文、译文) cascade-MVSNet——CVPR-202(源码、原文、译文) cascade-...
Ajax-Cascade-Ajax-Select.zip,这是一个现成的html/jquery源代码,演示了如何构建基于ajax的级联选择。,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建...
haarcascade-frontalface-alt人脸识别分类器文件
Free~, "opencv安装目录"\sources\data\haarcascades\"你想要的都有" 相应的还有lbp,当然实际生活场景这些官方提供的,效果都不好,请自行训练! haar的误检测率很高! lbp误检测高不说,框的方式也不好,不适合...
opencv识别车牌haarcascade-licence-plate-rus-16stages.xml
级联式无刷双馈发电机的仿真研究Cascade brushless doubly-fed generator simulation
OpenCV人脸识别文件haarcascade-frontalface-default.xml
OpenCV做人脸检测时,haarcascade-frontalface-default.xml
Open_CASCADE学习笔记-曲面建模
haarcascade_eye_tree_eyeglasses.xml OpenVC haarcascade_frontalcatface.xml haarcascade_frontalcatface_extended.xml haarcascade_frontalface_alt.xml haarcascade_frontalface_alt_tree.xml haarcascade_...
它使用图形界面来设置参数,并易于使用OpenCV工具来训练和测试分类器。 在本节中,描述了Cascade Trainer GUI的不同部分和功能。请注意,通过将鼠标移到图形界面上的每个元素,您将在底部状态栏上获得对其的简短...
Hibernate中Cascade和inverse的区别,讲解的很详细
串级控制系统实例程序,适合于初学者!!!!!!!!!!!!
Hibernate中cascade与inverse属性详解
eguid专栏中javacv进阶opencv图像处理和图像识别专栏文章使用的基于haar级联分类的正面人脸特征检测
CMOS Cascade Sigma-Delta Modulators for Sensors and Telecom
人脸harr级联分类器保存的.xml文件,包括:haarcascade-frontalface-default.xml等文件
NHibernate Inverse & Cascade