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.  [ 3 posts ] 
Author Message
 Post subject: Finding an inverse relation in the Hibernate metadata
PostPosted: Tue Sep 07, 2010 6:54 am 
Newbie

Joined: Mon Sep 06, 2010 9:15 am
Posts: 4
Hi,

Hibernate exposes its data model through the Configuration object, where I can access PersistentClass and Property objects. Some of the Property objects describe a relation between two PersistentClass instances.

For example, if I have a one-to-many relation between Person and Address in my model, and a many-to-one inverse relation between Address and Person, how can I find out that the two Property objects describing these relations belong together?

Thanks for your help!

Regards, Jochen


Top
 Profile  
 
 Post subject: Re: Finding an inverse relation in the Hibernate metadata
PostPosted: Tue Sep 07, 2010 2:43 pm 
Newbie

Joined: Mon Aug 02, 2010 11:14 pm
Posts: 10
Hey there Jochen,

I have never answered a question on any forums before but have been working on hibernate for the last 9 months or so. So bare with me. :)

I am almost certain you may be asking for something a lot more complicated than what I think you are asking. But for what I think you are asking, let me answer you this way.

Most inverse relationships for a one-to-many relationship are done through the mapped-by attribute of the many-to-one side of things. here's a code snippet based on your example.

Code:
@Table("person")
public class Person
{
...
List personAddresses;

...
@OneToMany(...) //using the forward join by using column to join...
public List getPersonAddresses()
{
  return address;
}
...

...
}


then on the inverse side

Code:
public class Address
{
...
private Address personWhoseAddressThisIs;
...
@ManyToOne(mappedBy="personAddresses")
public Address getPersonWhoseAddressThisIs()
{
   return personWhoseAddressThisIs;
}
...
...
}


That should do it, so when you retrieve person all the list of address will be in there and when you retrieve address the person whose address it is, will be residing inside your java class.

So hope this helps. If this isn't what you were asking for feel free to elaborate and maybe we could work towards a solution together. :)

Regards,

Ramon


Top
 Profile  
 
 Post subject: Re: Finding an inverse relation in the Hibernate metadata
PostPosted: Tue Sep 07, 2010 5:05 pm 
Newbie

Joined: Mon Sep 06, 2010 9:15 am
Posts: 4
Basically, this is how my data model looks. When I create a Hibernate Configuration (or AnnotationConfiguration), I can access the metadata for this model (information about the mapped classes and properties).

I now can retrieve a description of e.g. Person as an object of class org.hibernate.mapping.PersistentClass. The object in turn provides information about the Person's properties by exposing org.hibernate.mapping.Property objects (e.g. for personAddresses).

What I need is a way to identify which of these Property objects belong together, i.e. which property describes the inverse relationship of the other.

Regards,
Jochen


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