Hi All,
I am challenged with this problem for a week and not able to resolve.
Help me please..........
Problem description :
1. With following code as is, all child records are getting null IDs
(like USER.FIRM_ID, GROUP.FIRM_ID, ROLE.USER_ID, ROLE.GROUP_ID)
2. If I set "inverse=false", then it works, but that is not good enough
for me, because looks like hibernate inserts them witn null initially
so I am not able to set the IDs in children not null.
Please help
Datamodel, Mapping and Java Code is as given below
Code:
DATA MODEL
User
|-----------|
Firm | USER_ID |---|
|-----------| | USER_NAME | |
| FIRM_ID |---|---> | FIRM_ID | | Role
| FIRM_NAME | | |-----------| | |------------|
|-----------| | | | ROLE_CODE |
| Group |---->| USER_ID |
| |------------| |-->| GROUP_ID |
| | GROUP_ID |----| |------------|
| | GROUP_NAME |
|---> | FIRM_ID |
|------------|
Firm to User : One to Many
Firm to Group : One to Many
User to Role : One to Many
Group to Role : One to Many
Code:
Firm
----
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.domain.Firm" table="firm">
<id name="firmId" column="FIRM_ID" type="java.lang.Long" unsaved-value="0">
<generator class="native" />
</id>
<property name="firmName" column="FIRM_NAME"/>
<bag name="groups" lazy="true" cascade="save-update" inverse="true" >
<key column="FIRM_ID" />
<one-to-many class="com.domain.Group"/>
</bag>
<bag name="user" lazy="true" cascade="save-update" inverse="true" >
<key column="FIRM_ID" />
<one-to-many class="com.domain.User" />
</bag>
</class>
</hibernate-mapping>
Code:
public class Firm {
private Long firmId=Long.parseLong("0");;
private String firmName;
private List<Group> groups=new ArrayList<Group>();
private List<User> users=new ArrayList<User>();
public Long getFirmId() {
return firmId;
}
public void setFirmId(Long firmId) {
this.firmId = firmId;
}
public String getFirmName() {
return firmName;
}
public void setFirmName(String firmName) {
this.firmName = firmName;
}
public void addGroup(Group group)
{
this.groups.add(group);
group.setFirm(this);
}
public void addUser(User user)
{
this.users.add(user);
user.setFirm(this);
}
}
Code:
Group
-----
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.domain.Group" table="group">
<id name="groupId" column="GROUP_ID" unsaved-value="0">
<generator class="native" />
</id>
<property name="groupName" column="GROUP_NAME"/>
<property name="firmId" column="FIRM_ID"/>
<bag name="roles" lazy="true" cascade="save-update" inverse="true" >
<key column="group_id" />
<one-to-many class="com.domain.Role" />
</bag>
<many-to-one name="firm" column="FIRM_ID" not-null="true" insert="false" update="false" cascade="save-update"/>
</class>
</hibernate-mapping>
Code:
public class Group {
private Long groupId=Long.parseLong("0");;
private String groupName;
private Long firmId=null;
private Firm firm=new Firm();
private List<Role> userRoles=new ArrayList<Role>();
public Long getGroupId() {
return groupId;
}
public void setGroupId(Long groupId) {
this.groupId = groupId;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public Long getFirmId() {
return firmId;
}
public void setFirmId(Long firmId) {
this.firmId = firmId;
}
public void addRole(role role)
{
this.roles.add(userRole);
role.setGroup(this);
}
}
Code:
User
----
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.domain.User" table="user" >
<id name="userId" column="USER_ID" type="java.lang.Long" unsaved-value="0" >
<generator class="native" />
</id>
<property name="userName" column="USER_NAME"/>
<bag name="userRoles" lazy="true" cascade="save-update" inverse="true" >
<key column="user_id" />
<one-to-many class="com.domain.Role" />
</bag>
<many-to-one name="firm" column="FIRM_ID" not-null="true" insert="false" update="false" cascade="save-update"/>
</class>
</hibernate-mapping>
Code:
User Java code
--------------
Very similar to Group
Code:
public class Role {
private Long roleId=Long.parseLong("0");
private String roleCode;
private Long groupId=null;
private Long userId=null;
private Group group=new Group();
private User user=new User();
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Code:
-------------------------------
in FirmDao i have
hibernateTemplate.save(firm);
in RoleDao i have
hibernateTemplate.save(role);
etc
-------------------------------
in service layer
Role role=new Role();
role.setRoleCode("ADMIN");
User user=new User();
user.setUserName("TestName");
user.addRole(role);
Group group=new Group();
group.setGroupName("TestGroup");
group.add(group);
Firm firm=new Firm();
firm.setFirmName("TestFirm")
firm.addGroup(group)
firm.addUser(user);
firmDao.save(firm);
//Tried roleDao.save(role);