Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:2.17
oracle script
Code:
CREATE TABLE WORKFLOW_ROLE_GROUP
(
GROUP_ID INTEGER NOT NULL,
ORG_GROUP VARCHAR2(50),
ROLE_GROUP_NAME VARCHAR2(50) NOT NULL,
REMARK VARCHAR2(100)
)
TABLESPACE SYSTEM
PCTUSED 40
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
FREELISTS 1
FREELIST GROUPS 1
BUFFER_POOL DEFAULT
)
LOGGING
NOCACHE
NOPARALLEL;
CREATE UNIQUE INDEX PK_WORKFLOW_ROLE_GROUP ON WORKFLOW_ROLE_GROUP
(GROUP_ID)
LOGGING
TABLESPACE SYSTEM
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
FREELISTS 1
FREELIST GROUPS 1
BUFFER_POOL DEFAULT
)
NOPARALLEL;
ALTER TABLE WORKFLOW_ROLE_GROUP ADD (
CONSTRAINT PK_WORKFLOW_ROLE_GROUP PRIMARY KEY (GROUP_ID)
USING INDEX
TABLESPACE SYSTEM
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
FREELISTS 1
FREELIST GROUPS 1
));
CREATE TABLE WORKFLOW_ROLE_RELATION
(
GROUP_ID INTEGER NOT NULL,
ROLE_ID INTEGER NOT NULL,
ROLE_NAME VARCHAR2(50) NOT NULL,
ORG_CODE VARCHAR2(50) NOT NULL,
REMARK VARCHAR2(100)
)
TABLESPACE SYSTEM
PCTUSED 40
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
FREELISTS 1
FREELIST GROUPS 1
BUFFER_POOL DEFAULT
)
LOGGING
NOCACHE
NOPARALLEL;
CREATE UNIQUE INDEX PK_WORKFLOW_ROLE_RELATION ON WORKFLOW_ROLE_RELATION
(ROLE_ID, GROUP_ID)
LOGGING
TABLESPACE SYSTEM
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
FREELISTS 1
FREELIST GROUPS 1
BUFFER_POOL DEFAULT
)
NOPARALLEL;
ALTER TABLE WORKFLOW_ROLE_RELATION ADD (
CONSTRAINT PK_WORKFLOW_ROLE_RELATION PRIMARY KEY (ROLE_ID, GROUP_ID)
USING INDEX
TABLESPACE SYSTEM
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
FREELISTS 1
FREELIST GROUPS 1
));
ALTER TABLE WORKFLOW_ROLE_RELATION ADD (
CONSTRAINT FK_WORKFLOW_ROLE_RELATION FOREIGN KEY (GROUP_ID)
REFERENCES WORKFLOW_ROLE_GROUP (GROUP_ID));
Mapping documents:
[b] the mapping file for parent table:<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/ http://www.hibernate.org/-->
<class
name="com.itown.workflow.po.RoleGroup"
table="WORKFLOW_ROLE_GROUP"
>
<id
name="groupId"
type="java.lang.Integer"
column="GROUP_ID"
>
<generator class="sequence">
<param name="sequence">WORKFLOW_ROLE_GROUP_SEQ</param>
</generator>
</id>
<property
name="orgGroup"
type="java.lang.String"
column="ORG_GROUP"
length="50"
/>
<property
name="roleGroupName"
type="java.lang.String"
column="ROLE_GROUP_NAME"
not-null="true"
length="50"
/>
<property
name="remark"
type="java.lang.String"
column="REMARK"
length="100"
/>
<!-- Associations -->
<!-- bi-directional one-to-many association to WorkflowRoleRelation -->
<set
name="roleRelations"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
>
<key>
<column name="GROUP_ID" />
</key>
<one-to-many
class="com.itown.workflow.po.RoleRelation"
/>
</set>
</class>
</hibernate-mapping>
the mapping file for child table :<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/ http://www.hibernate.org/-->
<class
name="com.itown.workflow.po.RoleRelation"
table="WORKFLOW_ROLE_RELATION"
>
<composite-id name="comp_id" class="com.itown.workflow.po.RoleRelationPK">
<key-property
name="groupId"
column="GROUP_ID"
type="java.lang.Integer"
length="22"
/>
<!--
<key-many-to-one class="com.itown.workflow.po.RoleGroup" name="groupId" >
<column name="GROUP_ID" />
</key-many-to-one>
-->
<key-property
name="roleId"
column="ROLE_ID"
type="java.lang.Integer"
length="22"
/>
</composite-id> <property
name="roleName"
type="java.lang.String"
column="ROLE_NAME"
not-null="true"
length="50"
/>
<property
name="orgCode"
type="java.lang.String"
column="ORG_CODE"
not-null="true"
length="50"
/>
<property
name="remark"
type="java.lang.String"
column="REMARK"
length="100"
/>
<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- bi-directional many-to-one association to WorkflowRoleGroup -->
<many-to-one
name="roleGroup"
class="com.itown.workflow.po.RoleGroup"
update="false"
insert="false"
>
<column name="GROUP_ID" />
</many-to-one>
<!-- end of derived association(s) -->
</class>
</hibernate-mapping>
[/b]
my business is to save the parent table and child table at same time,but the child pojo can't be persisted after we run the following code ,why and how to fix the problem
Code:
if(jtRoleGroups.getSelectedRow() ==-1 || jtRoles.getModel().getRowCount()==0)
{
return;
}
Vector parentDataRow =(Vector) ((DefaultTableModel)jtRoleGroups.getModel()).getDataVector().get(jtRoleGroups.getSelectedRow());
RoleGroup roleGroup = toRoleGroup(parentDataRow);
Set roleRelations = new HashSet();
Vector children= ((DefaultTableModel)jtRoles.getModel()).getDataVector();
for(int i=0;i<children.size();i++)
{
Vector dataRow = (Vector)children.get(i);
RoleRelation roleRelation = toRoleRelation(dataRow);
roleRelation.setRoleGroup(roleGroup);
roleRelation.getComp_id().setGroupId(roleGroup);
roleRelations.add(roleRelation);
}
roleGroup.setRoleRelations(roleRelations);
try
{
DataModel.getInstance().saveOrUpdate(roleGroup);
}
catch(Exception ex)
{
ex.printStackTrace();
}
private static RoleRelation toRoleRelation(Vector dataRow)
{
RoleRelation roleRelation = new RoleRelation();
RoleRelationPK pk = new RoleRelationPK();
pk.setRoleId(dataRow.get(0)==null?null:Integer.valueOf(dataRow.get(0).toString()));
roleRelation.setComp_id(pk);
roleRelation.setRoleName(dataRow.get(1)==null?null:dataRow.get(1).toString());
roleRelation.setOrgCode(dataRow.get(2)==null?null:dataRow.get(2).toString());
roleRelation.setRemark(dataRow.get(3)==null?null:dataRow.get(3).toString());
return roleRelation;
}
Code: