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.  [ 9 posts ] 
Author Message
 Post subject: ID 0 should be a null reference
PostPosted: Sat Nov 13, 2004 3:07 pm 
Newbie

Joined: Fri Mar 26, 2004 9:52 pm
Posts: 3
I have a really simple problem where I want to create hierarchical "Category" where I'm using a legacy mysql database structure. (I searched the forums for a similar problem but couldn't find anything)

The current structure defines a Category as being a root Category if the parent_category column value is 0.

So in my hibernate mapping I have the following:

Code:
   <class name="com.mydomain.myproject.Category" table="wp_categories" >
        <id name="id" column="cat_ID" type="java.lang.Long" unsaved-value="0">
            <generator class="identity"/>
        </id>
      <property name="name" column="cat_name" type="java.lang.String" />
      <property name="description" column="category_description" type="java.lang.String" />
      <many-to-one name="parent" column="category_parent" />
   </class>



Is there a way to define the many-to-one mapping such that it the parent object will be null? I keep getting a "UnresolvableObjectException: No row with the given identifier exists: 0"

I would rather not redefine my mysql database because it's a legacy structure and I am still planning on using the older software that accesses it.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2004 7:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
This is an application level business rule which is not relational - you need to manage the association from that layer not at the mapping level, eg, an appropiate DAO will do a load on the from the property value.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2004 7:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Alternatively, you could code up a user type but i'm not sure it worth the effort.


Top
 Profile  
 
 Post subject: Errr...
PostPosted: Sat Nov 13, 2004 9:04 pm 
Newbie

Joined: Fri Mar 26, 2004 9:52 pm
Posts: 3
I'm not sure that actually solves my problem... or at least I'm not following your statement.

Under the assumption that the "Category" is a class where "parent" is a member variable of type "Category" and also following the assumption that the legacy database is using "0" to mean "NULL" mean in that a category with a "0" parent is a root object.

Is there any way that I can create a mapping that

I'm expecting a "You can't. You have to modify your database structure or explicitly capture the parent ID and then query the DB with the ID appropropiate ID value".

though I'm hoping there might be a means of mapping a value as a null association.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 14, 2004 5:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Your expectation is correct unless you code a custom user type. So it can be done but you have to put some work into it.


Top
 Profile  
 
 Post subject: Ya, I just discovered UserType
PostPosted: Sun Nov 14, 2004 5:40 pm 
Newbie

Joined: Fri Mar 26, 2004 9:52 pm
Posts: 3
I now know what you mean my UserType. I'll have to look into it. (Apparently it's used for enumerations as well).

Jeremy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 2:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Correct. It is actually a very powerfull aspect once you comfortable with the plumbing involved.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 3:56 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
hi,

for using '0' as 'null' you can take a look at this thread: http://forum.hibernate.org/viewtopic.php?t=935678&highlight=

There you can even find an example-UserType.

gtx
curio


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 15, 2004 6:04 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Hi curio & co

Thanks a lot for posting your thoughts in thread: http://forum.hibernate.org/viewtopic.php?t=935678I asked the very same thing already twice in the forum without getting a usefull answer.

I've also a similar but more complicated problem. In order no to capture this thread I opened a new one at http://forum.hibernate.org/viewtopic.php?t=936260

Any ideas are very welcome.
TIA
Ernst


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