-->
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.  [ 7 posts ] 
Author Message
 Post subject: Recursive Elements in Table -> PropertyValueException
PostPosted: Thu Jul 26, 2007 5:35 am 
Newbie

Joined: Thu Jul 26, 2007 5:12 am
Posts: 14
Location: Nepal
im pretty new with Hibernate..
I have a table named accountHeadGroups
Code:
id   parentId  code   name
1     0            a        abc
2     0            b        bmn
3     1            c        fffff
4     3            d        gggg


(abc(root) -> fffff (child) -> gggg (child))
if the parentId is zero then it is the root element.


Code:
<hibernate-mapping>
  <class name="pojo.AccountHeadGroup" table="accountHeadGroups">
   <id name="id" type="int" column="id" >
   <generator class="increment"/>
  </id>

  <set name="children" inverse="true" cascade="all-delete-orphan" lazy="true" order-by="id" >
    <key column="parentId" not-null="true" />
    <one-to-many class="pojo.AccountHeadGroup" />
    </set>

    <many-to-one name="parent"
          column="parentId"
          class="pojo.AccountHeadGroup"         
          not-null="true"
          insert="true"
          update="true"
          lazy="false"
          not-found="ignore"
    />

  <property name="code">
    <column name="code"/>
  </property>
  <property name="name">
    <column name="name"/>
  </property>
</class>
</hibernate-mapping>


Now, if I want to add a new AccountHeadGroup then
AccountHeadGroup ahg = new AccountHeadGroup();
ahg.setParent(someParent);
ahg.setCode("c");
ahg.setName("name");
session.save(ahg); and it'll work fine

but when I want to add/edit an AccountHeadGroup myGroup, whose parentId is 0 (root element), then myGroup's parent would be null.. and if I try to saveOrUpdate myGroup, it will throw org.hibernate.PropertyValueException: not-null property references a null or transient value: pojo.ItemGroup.parent

any advice????

_________________
:: there are 10 types of people in this world...1 who understands binary and the other 1 who does not ::


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 8:17 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You've specified in the mapping file that the parent is not-null="true". However, you _are_ expecting the parent entity to be null in some circumstances so the mapping doesn't match what will happen in the java.

- try setting not-null="false"

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 8:26 am 
Newbie

Joined: Thu Jul 26, 2007 5:12 am
Posts: 14
Location: Nepal
setting it to not-null false will set the parentId NULL in the database, which is not what I want.
actually, i want the parentId to be 0 if there is no parentGroup (parentId 0 means it is in the first level)

_________________
:: there are 10 types of people in this world...1 who understands binary and the other 1 who does not ::


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 8:58 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I don't think you can force the value to be set to 0 when the parent is null. Why do you need 0 rather than null anyway? This seems a little unusual.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 10:28 am 
Newbie

Joined: Thu Jul 12, 2007 12:52 pm
Posts: 5
Location: Boise, Idaho USA
You can just create a node that 'represents' null, i.e.:

Code:
id   parentId  code   name
0     0            null     null
1     0            a        abc
2     0            b        bmn
3     1            c        fffff
4     3            d        gggg


That way no node, including the "null node," actually has a null value as the parent.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 10:35 am 
Newbie

Joined: Thu Jul 26, 2007 5:12 am
Posts: 14
Location: Nepal
good idea, but then I have to modify all my codes as "from accountHeadGroups a WHERE a.id > 0" :-(

I think I should go back to my good old JDBC days! ;)

_________________
:: there are 10 types of people in this world...1 who understands binary and the other 1 who does not ::


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 10:50 am 
Newbie

Joined: Thu Jul 12, 2007 12:52 pm
Posts: 5
Location: Boise, Idaho USA
Instead of modifying the HQL query you could just apply a filter that would work just the same but with less code rewrite.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.