我有2个表,Receita(id,title,author),Tag(id_tag,tags)并自动创建Receita_Tag(idReceita,idTag)。
在Receita Class:
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="receita_tag", joinColumns={@JoinColumn(name="idReceita")},
inverseJoinColumns={@JoinColumn(name="idTag")})
@Cascade(org.hibernate.annotations.CascadeType.ALL)
private Collection<Tag> tag = new ArrayList<Tag>();
在Tag类中:
@ManyToMany(fetch=FetchType.LAZY, mappedBy = "tag")
@JoinTable(name="receita_tag", joinColumns={@JoinColumn(name="idTag")},
inverseJoinColumns={@JoinColumn(name="idReceita")})
@Cascade(org.hibernate.annotations.CascadeType.ALL)
private Collection<Receita> receita = new ArrayList<Receita>();
如何使用从两个表中返回所有数据的Receita的“id”进行查询?
我尝试了一些东西,但不起作用:
public Receita getReceita(int id) {
return (Receita) session.getCurrentSession().createQuery("from Receita r join r.tag t where r.id="+id+"=:id");
}
感谢大家的帮助。