The problem is that @OneToMany
can only have as inverse @ManyToOne
, and not @OneToOne
. @OneToOne
can only have as mappedBy another @OneToOne
, check the javadoc for those methods.
The mapping bellow gives you the meaning you want: one user can have many addresses, but an address can only refer to only one user using the id in the foreign key specified in @JoinColumn
:
@Entity
public class User {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
protected Long id;
...
@OneToMany(mappedBy="user")
private Set<Address> adresses;
}
@Entity
public class Address {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
protected Long id;
...
@ManyToOne(optional=false)
@JoinColumn("your_foreign_key_name")
private User user;
}