Hibernate : How to map a list of values of one attribute ? ( @OneToMany / @ManyToOne )

If you want for example mapping the atribute State of  Country entity. Any way, for ONE Country you can have MANY States, so :

@Table(name = "state")
public class State {
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_country")
private Country country;

}

@Table(name = "state")
public class Country {
@OneToMany(mappedBy = "country", 
    fetch = FetchType.LAZY, 
    cascade = CascadeType.ALL, 
    orphanRemoval=true)
private List<State> states = new ArrayList<>();

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s