学习休眠。我有以下类User、Region、Country,如下所示
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
}
我面临的问题是hibernate查询只返回区域而不返回国家。这可能是什么原因造成的?
已尝试获取国家和地区值,如下所示
System.out.println("Country: "+user.getCountry().getName());
System.out.println("Region: "+user.getRegion().getName());
来自Hibernate显示sql的响应。似乎缺少国家的详细信息。
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=?
发布于 2016-04-11 14:32:54
这是一个无效的映射。在使用Hibernate创建模式时,我在Hibernate 5中遇到了这个错误。
org.hibernate.boot.spi.InFlightMetadataCollector$DuplicateSecondaryTableException:
Table with that name [user_country_region] already associated with entity
无论如何,如果您可以在您的Hibernate版本中使用这种映射,那么将这种映射与两个关系的连接表一起使用很容易出错。
只需使用此映射通过外键列将User
与Country
和Region
关联。
public class User {
@OneToOne
private Country country;
@OneToOne
private Region region;
}
https://stackoverflow.com/questions/36550367
复制相似问题