-->
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.  [ 14 posts ] 
Author Message
 Post subject: Need help for a 1,0 Mapping association
PostPosted: Wed Aug 25, 2004 6:40 am 
Newbie

Joined: Wed Aug 25, 2004 6:28 am
Posts: 7
Help ! ;-)

I want to design a very simple mapping scenario with Hibernate and I get some pb.

I have 2 tables with a 0-1 association

Item --> ItemDetail

Here are my tables :

Item
--------------------------
ITEM_ID Number()
MyText Varchar()

ItemDetail
----------------------------
ITEMD_ID Number()
MyDetails Varchar()
ITEM_ID FK on ITEM_ID Table

My POJOs:
Item {
getID();
String getMyText();
<b> ItemDetail get/setItemDetail();</b>
}

ItemDetail {
getID();
String get/setMyDetails();
}

By default hibernate requires that you create an FK column on the Parent class. I don't want to do this because I don't want to deal with null FK column issue (the associated object ItemDetail can be null) in Item table.

Is it possible to move this FK column in the ItemDetail table and only indicate in the hibernate mapping file that the FK column is not in Item ?

Please I prefer not using bidirectionnal association or complex scenarios...

Thanks a lot.

Rabby


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 6:59 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I think I didn't understand what you were saying about "nulls" and "FKs", but what you are looking for might be this:

Code:
<class name="Item">
    <id column="ITEM_ID">...
    <many-to-one colum="FK_ITEM_DETAIL_ID" not-null="false"/>
</class>

<class name="ItemDetail">
    <id column="ITEM_DETAIL_ID">...
</class>

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 8:11 am 
Newbie

Joined: Wed Aug 25, 2004 6:28 am
Posts: 7
Hello Christian (thanks for the reply),
I prefer not inserting a FK_ITEM_DETAIL_ID column in my ITEM table because it is a 1,0 association and I will deal with such datas in my tables :

ITEM Detail FK_ITEM_DETAIL_ID
1 "My Item" null
2 "My Other Item" null
3 "My OO OTher Item" 1

ITEM_DETAIL
1 "My Item Detail"

You see what I mean ? This is better to have a FK in the ITEM_DETAIL table like following :

ITEM Detail FK_ITEM_DETAIL_ID
1 "My Item"
2 "My Other Item"
3 "My OO OTher Item"

ITEM_DETAIL FK_ITEM_ID
1 "My Item Detail" 1

How can I do that ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 8:12 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Your data and my mapping work very well.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 9:16 am 
Newbie

Joined: Wed Aug 25, 2004 6:28 am
Posts: 7
I have certainly missed something. I have cut and paste your mapping file :
Code:
<class name="Item" table="ITEM" >
   <id name="ID" column="ID" type="long">
      <generator class="sequence">
         <param name="sequence">SEQ_ID_ETA</param>
      </generator>
   </id>
   <property name="libelle" column="libelle" type="string" />   
   <many-to-one name="itemDetail" column="FK_ITEM_DETAIL_ID" not-null="false"/>
</class>

<class name="ItemDetail" table="ITEM_DETAIL" >
   <id name="ID" column="ID" type="long">
      <generator class="sequence">
         <param name="sequence">SEQ_ID_ETA</param>
      </generator>
   </id>
   <property name="detail" column="detail" type="string" />
</class>


Here is my POJO :

Code:
public class Item {
   Long ID;
   String libelle;
   ItemDetail itemDetail ;
                // With all getter and setters
                ItemDetail get/setItemDetail()
}


public class ItemDetail {
   Long ID;
   String detail;
                // With all getters and setters
}

Here is the error which is normal, itemDetail is not mapped with a Foreign key in Item class but in ItemDetail class...

[HibernateUtil] closeSession()
<25 2004 15 h 02 CEST> <Error> <HTTP> <BEA-101017> <[ServletContext(id=34547968,name=webapp,context-path=/webap
Root cause of ServletException.
net.sf.hibernate.HibernateException: unmapped property: itemDetail
at net.sf.hibernate.persister.AbstractEntityPersister.getPropertyValue(Ljava.lang.Object;Ljava.lang.String;)

Does I missed something ?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 9:18 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Yes, you are not using the mapping you have shown. Please re-read the Parent/Child relationship chapter and other parts of the documentation. Your problem is trivial, but you got confused by "moving the FK over there because of NOT NULL", which doesn't make much sense...

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 9:41 am 
Newbie

Joined: Wed Aug 25, 2004 6:28 am
Posts: 7
Christian,
I have read many times the documentation (i have bought hibernate in action ebook) on Parent/Child topic. It is always specified that the foreign key should be placed in the Parent class (in my case Item). I don't understand why this is a requirement...

You are right that not-null constraint doesn't make sens here since I will always have a valid FK in ItemDetail table. My the problem is that I can't have any foreign key in my parent class.

So, how can i handle such data ? Why is it so trivial ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 10:04 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Again, step by step: Why can't you have a FK_DETAIL_ID column in ITEM? The reason you gave "because of nullable" doesn't make sense. It surely can be NULL, which means that an ITEM doesn't have a DETAIL.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 12:31 pm 
Newbie

Joined: Wed Aug 25, 2004 6:28 am
Posts: 7
Because the existing schema is already designed like this and we want to avoid to update the existing data just because hibernate can't handle a navigation parent/child with the foreign key in the child class (which is the traditionnal design adopted by our DBA to minimize null columns).

Imagine that you have more than 1.000.000 records in the ITEM table having one ITEM_DETAIL.
With your solution, I will have more that 999.999 records set to null in ITEM table while our actual schema have just 1 record in the ITEM_DETAIL table with a non-null FK pointing on ITEM.

Very bad.

Anyway, thanks for your answers...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 12:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you would explain yourself better, you would be less frustrated with the answers you receive.

Now, it just so happens that I can guess what you want. Though its pretty hard, given the way you've communicated it.

What you need is <one-to-one name=".." property-ref="..."/>. You will find this documented in the Hibernate manual.

Now, in Hibernate 2.1 there were some performance problems associated with this kind of mapping. These are fixed in the just-released Hibernate 3.0 alpha.

In future, please spend much more effort on proper explanations. TIA


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 3:46 pm 
Newbie

Joined: Wed Aug 25, 2004 6:28 am
Posts: 7
Gavin,
If each time I ask (this is the first and the last time) a question, I should pay the price of your arrogance, you should seriously consider not funding a commercial company (with Jboss or others). If I were a customer (and I am, I bought your book and try tro promote Hibernate in one of the biggest french company), I would leave you right after such a response.

I have posted the mapping file and the POJO code. What the hell can I do more than that ? And I already spend much effort trying to post in english which is not my mother language.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 4:35 pm 
Newbie

Joined: Tue Jan 27, 2004 3:26 pm
Posts: 18
myrabbit wrote:
Gavin,
If each time I ask (this is the first and the last time) a question, I should pay the price of your arrogance, you should seriously consider not

I have posted the mapping file and the POJO code. What the hell can I do more than that ? And I already spend much effort trying to post in english which is not my mother language.


myrabbit,

I completly can't understand Your irritation. I can't see anything bad in Gavin's answer. Reading Your question I did it understand after fourth post (earlier You even didn't mentioned that You don't want to change Your schema). Furthermore this is a free support forum and some people do their best to help other for free. If you want a more sophisticated support consider buing it (it's not so expensive). Just my $.02.

Artur


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 5:19 pm 
Newbie

Joined: Wed Aug 25, 2004 6:28 am
Posts: 7
Hello Artur,
Please re-read my post and you will see that I repeated twice that I don't want (and can't) to change my schema...

1st post
Quote:
(...) I don't want to do this (...)


another one :

Quote:
(...)My the problem is that I can't have any foreign key in my parent class...(...)


Don't know what to do more than that.

I am graceful about the free support (I have always thanked Christian) but disapointed about the last Gavin's response (and the lack of clean support of 1,0 association in Hibernate 2)

Anyway, let's forget it. Im also not obliged to use hibernate.

[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 25, 2004 5:22 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Account banned.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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