Reputation: 115
I'm new in hibernate and I want to solve some doubt. At first, I've been searching for some kind of entity that I want to understand how do I map my entity, but I didnt find nothing so "comum" and simple to understand and I need help with this.
I have a class named "client" and another called "adress" and other called "company", Client has a list of adress, and company just once, but how do I map this?
I say, when use @manytoone (mapped by) or other kind?
My class CLIENT:
@Entity
public class Client {
@Id
@GeneratedValue
private long id;
private String name;
//Adress
private List<Adress> adress;
}
My Class Adress:
@Entity
public class Adress{
@Id
@GeneratedValue
private Long id;
private String pobox;
}
My Class Company:
@Entity
public class Company{
@Id @GeneratedValue
private Long id;
private String name;
//Adress
private Adress adress;
}
Upvotes: 1
Views: 569
Reputation: 103135
An address is usually one to one. That is, a company would not share an address with another company, would it? So you can try:
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="address_id")
private Adress address;
Upvotes: 2