-->
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.  [ 5 posts ] 
Author Message
 Post subject: many-to-one not fetching "The One"
PostPosted: Wed May 16, 2007 11:52 am 
Newbie

Joined: Thu Feb 09, 2006 12:37 pm
Posts: 18
Hi all,
I have a problem, I have many problems, but right now the one hibernate problem I am having problems with, is that when I do a mapping from the child object to teh parent object (representing the one to many relationship in the DB) using the many-to-one property, the parent object isint fetched and thus when I try to use the parent object/property I get a null pointer exception.

Mapping documents:

Code:
   <class name="User" table="cmt_006_user">
      <id name="OID" column="OID" unsaved-value="null">
         <generator class="native">
         </generator>
      </id>     
      <version name="VID" column="VID" unsaved-value="negative" type="long"/>   
      <property name="updated" type="timestamp" column="Updated"></property>
      <property name="name" column="Name"></property>
      <property name="password" column="Password"></property>
      <property name="userRoleOID" column="UserRoleOID"></property>
      <property name="deleted" column="Deleted"/>
      <many-to-one name="userRole" class="com.orange.componentcatalogue.dm.UserRole" column="UserRoleOID" insert="false" update="false"/>
   </class>


The class User, has a UserRole property. I am trying to use the proerty thus:
Code:
if(!(user.getUserRole().getName().equals("admin")))

any ideas why this would happen? perhaps I am fundementally missing something?

Thanks
Gavin[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 1:24 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
How did the user get persisted to begin with? Was it assigned a userRole?

So you'd have to do user.setUserRole(role) and not only role.getUsers().add(user), for example, because the many side of the relationship is the one that manages the link.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 7:51 am 
Newbie

Joined: Thu Feb 09, 2006 12:37 pm
Posts: 18
Hi, I have now, i fouind that online last night amend my userRole mapping thus:

Code:
  <class name="UserRole" table="cmt_016_userrole">
      <id name="OID" column="OID" unsaved-value="null">
         <generator class="native">
         </generator>
      </id>     
      <version name="VID" column="VID" unsaved-value="negative" type="long"/>   
      <property name="updated" type="timestamp" column="Updated"></property>
      <property name="name" column="Name"></property>
      <property name="deleted" column="Deleted"/>
     
      <set name="users"  inverse="true" cascade = "all-delete-orphan" table="cmt_006_user">
         <key column="UserRoleOID"/>
         <one-to-many class="com.orange.componentcatalogue.dm.User"/>
      </set>
   </class>


However still getting the error, in the User class I have both get and set UserRole. In user role I have a set and get Users:

Code:
   public Set<User> getUsers()
   {
   return this._users;
   }
   
   public void setUsers(Set<User> users)
   {
   this._users = users;
   }


I didnt persist the row, as it is already in the DB, but I will be, although initial I didnt have the relationship mapping set up, which is what I am working on now.

So I am a bit lost, i admit i didnt realise i need to set up the many end, and I was dumb for not realising that, but the example I found is as above, and what you are saying, I think, is also consistant with the above.

Thanks
G[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 8:13 am 
Newbie

Joined: Thu Feb 09, 2006 12:37 pm
Posts: 18
Actual I am stupid, the reason why I was still getting the null point was because I was using an un-persistered class I had put on the session.

Basicly the user logged in, so a create a User class to represent that user, check the users details in the DB if they match put user on the session. Thus tehre was no role associated with the user. How dumb!!!

No I am getting:
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 8:58 am 
Newbie

Joined: Thu Feb 09, 2006 12:37 pm
Posts: 18
Sorted, I turned lazy loading off, and it works :)
bit of a hack? maybe, but it gets me to where I want to be, now I just have to make a choice of if I use springs inbuilt transactions for hibernate or OpenSessionInViewFilter/Interceptor or the HibernateInterceptor,

I am thinking the transactions, it seems more robust then trust myself :D

Thanks for all your help

G


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.