-->
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.  [ 4 posts ] 
Author Message
 Post subject: 2 many-to-one references with 'shared' keys
PostPosted: Tue Oct 26, 2004 5:41 pm 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
Hibernate version: 2.1.6

Hi,

i know, that my problem is not really a 'hibernate'-problem, but perhaps someone has an idea i don't had... and perhaps it's not possible to handle it 'automatically' ... or there's a workaround ...

I've got a Class A with some attributes:
Code:
/**
* many-to-one --> Class B
*   not-null=false
*   outer-join=true
*   insert=false
*   update=false
* -----------------------
* many-to-one --> Class C
*   not-null=false
*   outer-join=true
*   insert=true
*   update=true
*/
public class A {
   private int id;                // the pk
   private int attr1;
   private int attr2;
   private String attr3;
   private String attr4;

   ....
}


and a Class B and C with a composite-primary-key ...
Code:
public class B {
   private int attr1;            // part of composite-id
   private int attr2;            // part of composite-id
   private String attr3;       // part of composite-id

   ....
}
public class C {
   private int attr1;            // part of composite-id
   private int attr2;            // part of composite-id
   private String attr3;       // part of composite-id
   private String attr4;       // part of composite-id
   ....
}


Class A has references to:
* Class B --> attr1, attr2 and attr3 are columns in 'many-to-one'-relationship.
* Class C --> attr1, attr2, attr3 and attr4 are columns in 'many-to-one'-relationship.

My problem is, that 3 values are part of 2 references ... one many-to-one-reference contains of 3 columns and the other of 4.

Everything works fine ... i can insert, update and delete ... no problem. Load Class A works fine, too when an entry for Class B and Class C is available in DB.

The problem occurs when i try to load Class A and no entry for Class C is available ...
Hibernate tries to load Class C though 'outer-join' is set to 'true' and i will get an exception.
I think hibernate do try loading Class C because parts of the key-values are set (attr1, attr2 and attr3 are set because an entry for Class B is available) ...

My question is, if i can 'tell' hibernate some way, that it should not try to load Class C if not all 4 keys are set to 'not null', or much better to a defined value?
Perhaps i can extend an existing class? If so, what would be a good point to look at?

If there's no way (i don't think it's a 'good' reference-mapping) to load this relationship automatically, i will load it 'by hand' in my persistence-layer ... but, if possible, it would be great to let hibernate do this work ...

Every help is welcome :)

thx!
curio

P.S.: And no ... i'm not allowed to change the db ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 27, 2004 4:12 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
back again :)

after reading parts of my today arrived book 'hib' (thanks to christian and gavin for writing that book ;) ) and some tests, i think i can handle all this by using a "CompositeUserType" ... (i should have think about it earier ... but okay, i was thinking to complex ...)

I've written such an Type which is able to do all the stuff i need, but now i've got one problem left :

How do i use this 'CompositeUserType' in the Mapping-File?

My current Mapping (for Class C) looks something like this:
Code:
<class name="C" ...>
  <composite-id name="xy" class="C_Id">
    <key-property name="attr1" .... />
    ....
  </composite-id>


The only place where i can add my CompositeUserType is for an "key-property" ...
Code:
<class name="C" ...>
  <composite-id name="xy" class="B_Id">
    <key-property
        name="what will that be?!"
        type="CompositeUserType" .... />
   
    <!-- no more key-properties />

  </composite-id>

but that wouldn't work ...

Using only an "<id>"-Tag wouldn't work either ... and setting a type to the composite-id ... well that's not possible ...

I think it's only a small step ... but i'm not able to go it ... somewhere i'm thinking strange, because i've read in this forum that's possible (should be in the wiki) ... but till now, i wasn't able to find an example ...

thx
curio

P.S.: sorry, if that's a stupid rfm question, too ... but please answer if you know it ... or just give me a link or a page in the book ... that would be really great :) ... and if we will meet some day, your cocktail is paid ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 29, 2004 1:42 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
sorry, i don't want to annoy you ... but is it really not possible to use a CompositeUserType as type for the composite-id - element?

Or is the question just to stupid? :)

gtx
curio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 29, 2004 10:03 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
reactivation another thread ...

and no, i don't 'talk to myself' everytime ... ;)
but perhaps another hibernate-user is searching for a similar solution and is happy to read my attempts ...

I've reallized after some further experiences with UserTypes, that i was half on the wrong way.

I don't need a CompositeUserType which spans the hole composite-id ... i only need some 'simple' CustomUserTypes ... for each type one ...

I've created those UserTypes which will do everything i need ... so far so good.

The final step is, that hibernate tries to load Class C, even if attr4 is not set ... propably because the 3 other attributes are set. If i could make it, that all CustomUserTypes returns 'null' if attr4 is not set, all will be fine (i've tried it).

I only need a generic way to do this ...

My current solution (not very nice. There must be a better one WITHOUT patching hibernate):

attr4 is now the first part of the composite-id of Class C. It's the field which could be empty if all other fields are set, due to the relationship from Class A to Class B.
Therefore all other CustomUserTypes (for attr1, attr2 and attr3) can return 'null' if the CustomUserType for attr4 returns null (important: Only for the relation between ClassA and Class C!).
I've realized that using a ThreadLocal-Variable ... a little bit like the HibernateUtil-Class. Each UserType 'takes a look there' in the "nullSafeGet"-method before it does something ... if it founds 'attr4 was null', 'null' will be returned.

Advantage:
- i don't need to patch hibernate
- it works :) (with constraints - see disadvantage) Reference to Class B will be loaded ... Class C not ...

Disadvantage:
- without a good documentation no one will understand what i'm doing here ;)
- if there's more than one situation like this (during auto cascade from hibernate) it won't work because i haven't find a good point to "reset" my marker yet ...
- i don't like it

I would like it much better, if i can configure hibernate in a way, that a reference will only be loaded if each part of the composite-id is not null. Is this possible?

Perhaps someone has a better idea ... if i'll find one i propably will post it ...

gtx
curio

P.S.: by the way ... the only reason why i had to it is, that hibernate tries to load Class C in an seperate SQL-SELECT although i've set "outer-join=true".


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