-->
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: Can't figure out unidirectional one-to-one relationship
PostPosted: Tue Jul 27, 2004 12:14 pm 
Beginner
Beginner

Joined: Tue Jun 29, 2004 3:44 pm
Posts: 43
I have a class "A" that has a property of abstract class "B" in a one to one relationship. "A" will have only one "B", but the "B" will change from time to time to different subclasses. Any instantiated "B" subclass will only be related to one "A". "B" does not have a reference to "A".
Code:
public class A {
    String id;
    int version;
    B myB;
}

public abstract class B {
    String it; // all subclasses of B have the same properties, but different behavior
    abstract void doIT();
}

public class B1 {
    void doIT() {
        // differs
    }
}

Please tell me if I'm wrong in thinking that:
1. I cannot map "B" as a component of "A" because it is subclassed.
2. I cannot use a one-to-one with primary key associations without either:
2a. adding a property to "B" to know "A" (impossible in this situation),
or:
2b. having "A"'s id change every time it's "B" changes (very, very inconvenient because I need to refer to objects by their id).
3. I can use a many-to-many from "A" to "B"

I have successfully implemented #3 in my list, but have a second problem when I do. I cannot use cascade="all-delete-orphan" with a many-to-many relationship, so every instantiated "B" object is saved and never deleted. If an "A" goes through 12 different "B"s, all 12 remain in the database despite the object graph only needing one of them.

Does anyone have any suggestions about how I should map this so that I don't have many unnecessary rows in my "B" table?

Thanks for any suggestions,
Scott


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 12:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Did you try a custom type.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 12:38 pm 
Beginner
Beginner

Joined: Tue Jun 29, 2004 3:44 pm
Posts: 43
emmanuel wrote:
Did you try a custom type.


No, I haven't. I considered it, but it looks rather intimidating. Can you point me to some good documentation of how to write one?

Also, in case it influences your suggestion, the example I gave is rather simplified. Real "B"s have six or seven properties that are mostly other objects in my domain. I was under the impression that custom types were for simpler classes like enums?

Thanks a lot for your help,
Scott


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 9:10 am 
Beginner
Beginner

Joined: Tue Jun 29, 2004 3:44 pm
Posts: 43
I've searched through the online documentation and found no instructions or tutorial for creating complex custom types. Is there information about doing this anywhere?

Anyone have any other ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 9:19 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Try the Wiki community area and the search function on the website with "usertype".

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 9:36 am 
Beginner
Beginner

Joined: Tue Jun 29, 2004 3:44 pm
Posts: 43
[quote="christian"]Try the Wiki community area and the search function on the website with "usertype".[/quote]

Thank you. I have done that. There are two links to the FAQs that have answers of "Use a UserType." And two links to suggestions for avoiding potential problems with the normal use of UserType. The search returns nothing about CompositeUserType nor how to write a normal UserType.

The API says "A CompositeUserType may be used in almost every way that a component may be used. It may even contain many-to-one associations." Can CompositeUserType, unlike component, handle inheritance?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 9:38 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The searched returns several pages that show you how to implement different usertypes. Try again...

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 9:45 am 
Beginner
Beginner

Joined: Tue Jun 29, 2004 3:44 pm
Posts: 43
christian wrote:
The searched returns several pages that show you how to implement different usertypes. Try again...


You're absolutely right, there are 15, including the four I mentioned before. Another word or letter must have slipped into the search box before. Sorry for wasting your time with that last post.

I promise to read through these and have at least another cup of coffee before posting again :)

Thanks,
Scott


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.