【发布时间】:2014-05-22 09:20:01
【问题描述】:
我得到了这个 db 模型,我想通过特定语言的主要对象获取 Continent 和 Country。我写了一个成功返回国家/地区的 SQL 语句(包装在 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;
}
国家地区
@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);
}}
提前致谢!
【问题讨论】:
标签: sql hibernate hql hibernate-mapping