我得到了这个db模型,我希望得到Continent并通过主要对象获取特定语言的国家/地区。我写了一个成功返回国家的SQL sentece(包装在Object [] []上),但正如我上面所说,我需要Continent-> ContinentLocale-> Country-> CountryLocale,我无法编写正确的HQL
我使用Hibernate 4.3.5
SQL句子
select C.idcountry, CL.name,D.idcontinent,DL.name from country C
inner join country_locale CL on c.idcountry=cl.idcountry and cl.idlanguage=2
inner join continent D on c.idcontinent=D.idcontinent
inner join continent_locale DL on D.idcontinent=DL.idcontinent and DL.idlanguage=2;
Continent.java
@Entity
@Table(name="continent")
public class Continent implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="IDcontinent")
private Integer idContinent;
@OneToMany
@JoinColumn(name="IDcontinent")
private Set<Country> countries;
@OneToMany(mappedBy="primaryKey.continent")
private Set<ContinentLocale> continentLocale;
}
ContinentLocale.java
@Entity
@Table(name="continent_locale")
@AssociationOverrides({
@AssociationOverride(name = "primaryKey.continent", joinColumns = @JoinColumn(name = "IDcontinent")),
@AssociationOverride(name = "primaryKey.language", joinColumns = @JoinColumn(name = "IDlanguage"))
})
public class ContinentLocale implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ContinentLocalePK primaryKey=new ContinentLocalePK();
@Column(name="name",nullable=false,length=50)
private String name;
@Transient
public Continent getContinent(){
return getPrimaryKey().getContinent();
}
public void setContinent(Continent continent){
getPrimaryKey().setContinent(continent);
}
@Transient
public Language getLanguage(){
return getPrimaryKey().getLanguage();
}
public void setLanguage(Language language){
getPrimaryKey().setLanguage(language);
}
}
Country.java
@Entity
@Table(name="country")
public class Country implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name="IDcountry")
private Integer idCountry;
@OneToMany(mappedBy="primaryKey.country")
private Set<CountryLocale> countryLocale;
}
CountryLocale
@Entity
@Table(name="country_locale")
@AssociationOverrides({
@AssociationOverride(name = "primaryKey.country", joinColumns = @JoinColumn(name = "IDcountry")),
@AssociationOverride(name =“primaryKey.language”,joinColumns = @JoinColumn(name =“IDlanguage”)) }) 公共类CountryLocale实现Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private CountryLocalePK primaryKey=new CountryLocalePK();
@Column(name="name",nullable=false,length=50)
private String name;
@Transient
public Country getCountry(){
return getPrimaryKey().getCountry();
}
public void setCountry(Country country){
getPrimaryKey().setCountry(country);
}
@Transient
public Language getLanguage(){
return getPrimaryKey().getLanguage();
}
public void setLanguage(Language language){
getPrimaryKey().setLanguage(language);
}}
提前致谢!
答案 0 :(得分:0)
我只是找到了解决方案,并且对于上面映射的hql语句是:
获取国家/地区语言
HQL =“从大陆D内连接中选择不同的CL D.continentLocale DL内连接D.countries C内连接C.countryLocale CL其中CL.primaryKey.language.idLanguage = 2”
获取大陆区域设置
HQL =“从大陆D内部连接选择DL D.continentLocale DL其中DL.primaryKey.language.idLanguage = 2”