-->
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: Why Associations?
PostPosted: Thu Mar 11, 2010 8:12 am 
Newbie

Joined: Thu Nov 06, 2008 3:42 am
Posts: 15
Hi,
I have a basic question here. I couldn't understand the need for associations.
Would you please tell what we can achieve with associations which we can't if we don't use associations?

Kind Regards,
Sham.


Top
 Profile  
 
 Post subject: Re: Why Associations?
PostPosted: Thu Mar 11, 2010 8:51 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
You need Associations to establish relations-ships between persistent objects.
For example if you want map which Pets a determinate Persons owns,
then the best is to map it with an association
Code:
@Entity
class Person {

@OneToMany
protected Set<Pet> pets;
}


(How would you do implement it without assocciatons?)


Top
 Profile  
 
 Post subject: Re: Why Associations?
PostPosted: Thu Mar 11, 2010 9:01 am 
Newbie

Joined: Thu Nov 06, 2008 3:42 am
Posts: 15
Hi pb00067,

Thanks for the reply. I am writing her from my understanding, but not to contradict what you said.
The problem here is that I want pets belonging to one person if I haver person ID stored in Pets table as foreign key. I can query using HQL and get the pets
"from Pets pets where pets.personid = <personid>"

So may be the question. What are the pros if I use associations instead of this?
or
Is Associations used if there was no foreign key person ID in Pets table?

Kind Regards,
Sham.


Top
 Profile  
 
 Post subject: Re: Why Associations?
PostPosted: Thu Mar 11, 2010 10:59 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
The problem here is that I want pets belonging to one person if I haver person ID stored in Pets table as foreign key.


OK, then speak about bidirectional OneToMany relation

Code:
@Entity
class Person {

@OneToMany(mappedBy="person")
protected Set<Pet> pets;
}

@Entity
class Pet {
...
@ManyToOne()
protected Person person;

}



Quote:
I can query using HQL and get the pets
"from Pets pets where pets.personid = <personid>"

So may be the question. What are the pros if I use associations instead of this?



One of the pro's are:
it is much more comfortable to call
Code:
person.pets()

than
Code:
session.createQuery("from Pets pets where pets.personid = " + person.getId()).list()


Furthermore the collection of person.pets() can be configured to be cached into a 2Lcache implementation.
(For queries it is also possible but much lesser effective).

N.B.: How do you generate the schema with foreign keys without mapping associations ?
Is your database a pre-existing one which already has its fix schema?

Quote:
Is Associations used if there was no foreign key person ID in Pets table?


I don't understand this question.
Anyway if you map the association like I showed in the first post,
then you would not have generated any foreign key in pet-table.


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.