今天在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"?> <hibernate-mapping> |
2.
<?xml version="1.0" encoding="ISO-8859-1"?> <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)是不能被保存的。