-->
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: Associated class for one-to-one relations
PostPosted: Tue Apr 04, 2006 8:20 pm 
Newbie

Joined: Thu Dec 01, 2005 3:18 pm
Posts: 6
I am using the latest version of the Hibernate tools (3.1b4) and am trying to have my generated POJO to have the following set method for one-to-one relations:

Code:
public void setFoo(Foo foo) {
    this.foo = foo;
    if (foo.getBar() != this)
        foo.setBar(this);
}


With a many-to-one and a one-to-many relationship I am already able to generate code that looks like:

Code:
public void addToFoos(Foo aFoo) {
    getFoos().add(aFoo);
    aFoo.setBar(this);
}


I can generate the above code by doing various comparisons ant iterations using the mapped persistent class. Unfortunately there does not appear to be a similar mechanism for one-to-one relationships.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 9:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what is your starting point ?

database or mapping files ? (reverse engineering or basic code generation ?)

Reverse engineering does not build up one-to-one's so you will never get such things created in the metamodel.

mapping files (ofcourse) support one-to-one and then my question is what is the problem you are having ?

btw. if your addToFoos templates are good then please submit them to jira since it is generally usefull.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 12:03 am 
Newbie

Joined: Thu Dec 01, 2005 3:18 pm
Posts: 6
I'm starting from mapping files. We do pretty much everything from the hbm.xml files.

Unfortunately I'm not yet at liberty to provide any source code for the addToFoos. I expect that this will change sometime in the near future, at which point I can provide a patch/template code.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 6:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so if you start from mapping files what is the actual problem ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 11:10 am 
Newbie

Joined: Thu Dec 01, 2005 3:18 pm
Posts: 6
The problem is that I can't figure out how to get the corresponding field name. At source code generation runtime, the only information that I can find is the name of the other class in the one-to-one relationship.

Running through the Eclipse debugger, I can't find a path from the information I have about the property for the OneToOne object to find out the name of the opposing field.

I haven't found any mechanism to work from a class name as a String back to either a PersistentClass or a RootClass object. If I could get to either of these, this would be reasonably straightforward.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 11:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you will need to go to the "other side" and find a matching OneToOne via getReferencedEntityName.

How did you find the "opposite" side of the ManyToOne/OneToMany ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 1:04 pm 
Newbie

Joined: Thu Dec 01, 2005 3:18 pm
Posts: 6
I had found getReferencedEntityName, but couldn't figure out how to go from that String to a more useful object like a PersistentClass or even better a RootClass.

I was only concerned about one side of the ManyToOne/OneToMany relationship (i.e. that of the parent) and was able to do that by looking at the columns of the associated PersistentClass to determine which column was the relevant one. I am currently trying to determine whether I can actually post that code.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 05, 2006 2:38 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
theOtherSide = $cfg.getClassMapping(entityName);

_________________
Max
Don't forget to rate


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.