开发者

JPA relationship

I have created two tables as person and address using JPA. I want to give one-to-many relationship between these tables. If I give the following

 @OneToMany(ma开发者_运维百科ppedBy="address",targetEntity=person.class,fetch=FetchType.EAGER)

in the address table means it's not working correctly. Can any one help me?

Thanks in advance.


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).


this is an example

 @ElementCollection
@CollectionTable(name = "NUMBER")
private List<String> number;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜