-->
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.  [ 8 posts ] 
Author Message
 Post subject: UnresolvableObjectException
PostPosted: Wed Jun 23, 2004 6:10 pm 
Beginner
Beginner

Joined: Wed Jun 09, 2004 2:49 pm
Posts: 25
I have two table, say table A and table B,
table A has primary key uid (assigned) and B has also primary key
uid (assigned).

The reationship is: if there a row in table B, there is always a row of same
uid in table A. But if there is row in A, the same uid may not exist in table
B

So I have many-to-one mapping as follows (see below).

However, whenver I try get a instance of A using
session.find(), it *ALWAYS* tries to make an instance of B of same
uid. If there is no row exist in table B,
Hibernate throws UnresolvableObjectException.


for table A:

<class
name="A'
table="A"
>
<id
name="uid"
column="uid"
type="java.loang.Long"
<generator class="assigned"
</id>

<many-to-one
name="partner"
column="uid"
update="false"
insert="false"
class="B"
not-null="false"
/>

</class>



for table B:
<class
name="B"
table="B"
>
<id
name="uid"
column="uid"
type="java.loang.Long"
<generator class="assigned"
</id>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 23, 2004 6:49 pm 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Could you please post the query you are running in the session.find().


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 23, 2004 7:00 pm 
Beginner
Beginner

Joined: Wed Jun 09, 2004 2:49 pm
Posts: 25
The query is like this

session.find("from A as a where a.uid = ?", new Long(332), Hibernate.INTEGER);

I have tested uids that appear in both table without any problem.
The problem happens only when if a uid exists in table A, but not B.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 23, 2004 7:59 pm 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Well the problem is occuring because of the mapping. As you are mapping the primary key column of A as the many-to-one column to B, Hibernate is looking at the results it gets back and basically assuming that there is a partner associated with it, as there is a value in the uid column.

It would be better to map this as the one-to-one mapping it really is, and if you make it bidirectional, you can enforce the rules about a row in B having to have a row in A. The mappings would be as follows:

Code:
<class name="A" table="A">
    <id name="uid" column="uid" type="java.lang.Long">
        <generator class="assigned">
    </id>

    <one-to-one
        name="partner"
        class="B"
        cascade="none"
        outer-join="auto"
        constrained="false"
        not-null="false"
        />

</class>

<class name="B" table="B">
    <id name="uid" column="uid" type="java.lang.Long">
        <generator class="assigned">
    </id>

    <one-to-one
        name="partner"
        class="A"
        cascade="none"
        outer-join="auto"
        constrained="true"
        not-null="true"
        />

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 12:12 pm 
Beginner
Beginner

Joined: Wed Jun 09, 2004 2:49 pm
Posts: 25
I got it working. Thank you so much.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 7:16 pm 
Beginner
Beginner

Joined: Wed Jun 09, 2004 2:49 pm
Posts: 25
Sorry I still have one thing that need help:

How to put these mapping as XDoclet tag in java source code file?
Can you show me how to? or can you give a place that I can examples?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 4:48 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
In class A, in the JavaDoc above your getter (well that's where I usually put my mappings anyway):

Code:
/**
* @hibernate.id column="uid" generator-class="assigned" type="long"
*/
public Long getUid() { ... }

/**
* @hibernate.one-to-one class="B" constrained="false" cascade="none" outer-join="auto"
*/
public B getPartner() { ... }


In class B:

Code:
/**
* @hibernate.id column="uid" generator-class="assigned" type="long"
*/
public Long getUid() { ... }

/**
* @hibernate.one-to-one class="A" constrained="true" cascade="none" outer-join="auto"
*/
public A getPartner() { ... }


Depending upon how you have your configuration set-up, you may well need the full package declared for your classes in those mappings - if hibernatedoclet moans that it can't find the class, that's the problem.

Just as a warning, you should read the document on IDs, as using assigned identifiers changes how you save and update your entities - http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-id-assigned.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 5:31 pm 
Beginner
Beginner

Joined: Wed Jun 09, 2004 2:49 pm
Posts: 25
Thank You!!


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