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.