views:

32

answers:

1

Hai friends

I have created two table as person and address using jpa. I want to give one to many relationship between these tables. if i give the folowing

 @OneToMany(mappedBy="address",targetEntity=person.class,fetch=FetchType.EAGER)

in the address table means its not working correctly. cn any one help me.

Thanks in advance.

A: 

Without knowing anything about your architecture I will guess as to what you need.

JPA is smart enough to know how to join your tables so if you have id's in both tables you actually don't need to have "mappedBy" and "targetEntity".

You simply need to annotate your class as follows: (assuming your relationship is one address has many people).

Within the Address class:

@OneToMany
@JoinColumn(name="address_id")
public List<Person> getPeople()
{
    return people;
}

This will place address_id as a field in your person table representing their associated address. Since you are declaring your list of type Person JPA will know to map to the person table (as long as the Person class is annotated properly with @Entity).

kgrad
If the association is bi-directional, you definitely need `mappedBy` (or this would result in two uni-directional associations).
Pascal Thivent
@Pascal Thivent, just made the assumption that it was unidirectional based on zero information provided by the OP.If bidirectional you also have to have an annotated Address in the person class and all the other requirements...
kgrad