【问题标题】:Getting locale entities hibernate hql获取语​​言环境实体休眠 hql
【发布时间】: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


    【解决方案1】:

    我只是找到了解决方案,对于上面的映射 hql 句子是:

    获取国家/地区语言环境

    HQL="select distinct CL from Continent D inner join D.continentLocale DL inner join D.countries C inner join C.countryLocale CL where CL.primaryKey.language.idLanguage=2"

    获取大陆区域设置

    HQL="select DL from Continent D inner join D.continentLocale DL where DL.primaryKey.language.idLanguage=2"

    【讨论】:

      猜你喜欢
      • 2016-12-09
      • 2010-09-08
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多