-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Bidirectional inverse=true inserts null IDs in child
PostPosted: Wed Jun 17, 2009 6:07 pm 
Newbie

Joined: Wed Jun 17, 2009 5:44 pm
Posts: 3
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);


Top
 Profile  
 
 Post subject: Re: Bidirectional inverse=true inserts null IDs in child
PostPosted: Fri Jun 19, 2009 11:49 am 
Newbie

Joined: Mon Oct 27, 2008 2:43 am
Posts: 7
Hi,
try changing
Code:
<many-to-one name="firm" column="FIRM_ID" not-null="true" insert="false" update="false" cascade="save-update"/>

to
Code:
<many-to-one name="firm" column="FIRM_ID" not-null="true"/>


hope it helps.

_________________
-GJ


Top
 Profile  
 
 Post subject: Re: Bidirectional inverse=true inserts null IDs in child
PostPosted: Thu Jun 25, 2009 6:31 pm 
Newbie

Joined: Wed Jun 17, 2009 5:44 pm
Posts: 3
Thanks Gaurav for the response... But it does not work.. it gives error

column xx should be mapped with insert="false" update="false"


Top
 Profile  
 
 Post subject: Re: Bidirectional inverse=true inserts null IDs in child
PostPosted: Fri Jun 26, 2009 1:57 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
I guess it is because of inverse true.
add below property in mapping
column xx should be mapped with insert="false" update="false"

_________________
Dharmendra Pandey


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.