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.  [ 5 posts ] 
Author Message
 Post subject: bidirectional onetoone mapping, class used in multiple props
PostPosted: Thu Jan 28, 2010 9:01 am 
Newbie

Joined: Thu Jan 08, 2009 8:21 am
Posts: 5
Hello all,

I would like to accomplish a bidirectional mapping to a class used in multiple fields.

Example:

"The owner" (see http://docs.jboss.org/hibernate/stable/ ... e/#d0e1039):
Code:
public class House {

    @OneToOne (fetch = FetchType.EAGER)
    @Cascade ({CascadeType.PERSIST,CascadeType.MERGE,CascadeType.SAVE_UPDATE})
    @ForeignKey(name="FK_House_Gardener", inverseName="FK_Gardener_House")
    private HouseEmployee gardener;

    @OneToOne (fetch = FetchType.EAGER)
    @Cascade ({CascadeType.PERSIST,CascadeType.MERGE,CascadeType.SAVE_UPDATE})
    @ForeignKey(name="FK_House_Caretaker", inverseName="FK_Caretaker_House")
    private HouseEmployee caretaker;
}


"The not responsible class" (see http://docs.jboss.org/hibernate/stable/ ... e/#d0e1039):

Code:
public class HouseEmployee {

    // Will work, but only if this Employee is the gardener
    //@OneToOne(fetch = FetchType.EAGER, mappedBy="gardener")

    // Will work, but only if this Employee is the caretaker
    //@OneToOne(fetch = FetchType.EAGER, mappedBy="caretaker")

    @OneToOne(fetch = FetchType.EAGER, mappedBy="###what-to-enter?###")
    @Cascade ({CascadeType.PERSIST,CascadeType.MERGE,CascadeType.SAVE_UPDATE})
    private House house;
}


How can I accomplish that the "house"-property in HouseEmployee is filled in both cases?

Thank you very much for any hint!

Best regards
Stephan


Top
 Profile  
 
 Post subject: Re: bidirectional onetoone mapping, class used in multiple props
PostPosted: Thu Jan 28, 2010 11:34 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi Stephan,

sorry, but that what you want is rather weird.
Counterquestion:
What should in your opinion hibernate fill into employee1.house
if employee1 is gardener for house 1 and at also caretaker of house 2?


Top
 Profile  
 
 Post subject: Re: bidirectional onetoone mapping, class used in multiple props
PostPosted: Thu Jan 28, 2010 11:50 am 
Newbie

Joined: Thu Jan 08, 2009 8:21 am
Posts: 5
Hello pb00067,

thanks for your post. Your objection is certainly right. But in my world one employee can only work in one house.

To better clarification let's change our example:

Code:
public class Mother {
  private Child mostIntelligentChild;
  private Child mostChuckleheadedChild;
}

public class Child {
  private Mother mother;
}


Any more ideas?

Thanks again
Stephan


Top
 Profile  
 
 Post subject: Re: bidirectional onetoone mapping, class used in multiple props
PostPosted: Fri Jan 29, 2010 3:47 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I don't think that Hibernate supports that what you want, because it goes beyond any UML-specification.


Top
 Profile  
 
 Post subject: Re: bidirectional onetoone mapping, class used in multiple props
PostPosted: Wed Jul 28, 2010 9:34 am 
Newbie

Joined: Wed Jul 28, 2010 8:53 am
Posts: 1
Hi all,

I also need to map similar situations:
1) A Person (entity) has a PartnerShip (entity) with another Person. PartnerShip whould have properties 'rightPerson' and 'leftPerson' (and some others - otherwise there would be no need for this entity ;-).
2) A Report (entity) relates to a source Person (entity) and a target Person. There is only one Report for a given Person.

In UML that's the case where a class has 2 associations with the same class.

Additional requirement would be bidirectionality.

Question: Was is (are) the right way(s) to map such situations with Hibernate?

FYI:
In our current implementation (first case above), Person has properties 'rightPartnerEdge' and 'leftPartnerEdge' (0..1 cardinality but implemented as Set to get delete-orphan behaviour!).
It actually works but is quite complicated...

Thanks in advance for your response!

Best regards
Olivier


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