`
huiqinbo
  • 浏览: 336322 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

cascade="save-update" 与 inverse="true"使用讲解(回钦波)

阅读更多

今天在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
2
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics