I have the following classes User, Region, Country as follows, public class User {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @Column(name = "first_name") Private String firstName; @Column(name = "last_name") private String lastName; @OneToOne(fetch = FetchType.EAGER) @JoinTable(name = "user_country_region", joinColumns ={@JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "country_id") }) private Country userCountry;
@OneToOne(fetch = FetchType.EAGER) @JoinTable(name = "user_country_region", joinColumns = {@JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "region_id") }) private Region userRegion;
//With its respective Getters and Setters }
public class Country {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @Column(name = "name") private String name;
//With its respective Getters and Setters }
public class Region {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @Column(name = "name") private String name;
//With its respective Getters and Setters }
Tried getting country and region values as below
System.out.println("Country: "+user.getCountry().getName()); System.out.println("Region: "+user.getRegion().getName());
Hibernate Show SQL output as below
Hibernate: select this_.id as id1_3_3_, this_.first_name as first_na2_3_3_, this_.last_name as last_na3_3_3_, region2_.id as id1_8_2_, region2_.name as name2_8_2_ from user this_ left outer join user_country_region this_1_ on this_.id=this_1_.user_id left outer join region region2_ on this_1_.region_id=region2_.id where this_.id=?
Seems missing country details. Kindly assist.
|