HQL的多对多关系

时间:2014-07-16 08:41:01

标签: sql hibernate hql

我有一门语言和一个国家级课程。

@Entity
@Table(name = "countries")
@org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
@NamedQueries({
        @NamedQuery(name="Country.findLanguagesByCountryId",query="???")
})
public class Country implements IEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable = false, unique = true)
    private String name;       

    @ManyToMany(targetEntity = Language.class)
    @JoinTable(name = "country_language", joinColumns = {@JoinColumn(name = "cid")}, inverseJoinColumns = {@JoinColumn(name = "lid")})
    private Set<Language> languages;
}

@Entity
@Table(name = "languages")
public class Language implements IEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable = false, unique = true)
    private String name;
}

如您所见,国家/地区有语言列表,但语言没有国家/地区列表。

我需要HQL,它应该按国家/地区ID查找语言。

1 个答案:

答案 0 :(得分:0)

我想我已经找到了答案

select language from Country country
inner join country.languages language 
where country.id=?1