多对多的 Hibernate hql
Hbernate hql for many to many
我有两个 类 具有多对多关系
@Table(name="employe")
public class Employe implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int Id_emp;
@Column(unique=true)
private String username;
@ManyToMany
@JoinTable(name="produit_employe",joinColumns=@JoinColumn(name="id_employe"),inverseJoinColumns
=@JoinColumn(name="id_produit"))
private List<Produit> produits =new ArrayList<Produit>();
///}
Entity
@Table(name="Produit")
public class Produit implements Serializable {
@Id
private int id_produit;
private String nom_produit;
@ManyToMany(mappedBy="produits")
private List<Employe> employes=new ArrayList<Employe>();
//}
如何显示分配给每位员工的产品名称。
就像我用 sql 做的例子一样,我想把它转换成 hql。
select p.nom_produit from produit_employe pe, produit p where pe.id_produit=p.id_produit AND
pe.id_employe=2
在使用 hql 时,我们必须从实体的角度来思考,而不是像在 sql 中那样。
这相当于 hql 是:
select p.nom_produit from Employe e join e.produits p where e.Id_emp = 2
我已经使用 Spring Data JPA 对其进行了测试,这是员工存储库中使用的代码:
@Query("select p.nom_produit from Employe e join e.produits p where e.Id_emp = :employeId")
List<String> getProductNameAssignedToEmployee (@Param("employeId") Integer employeId);
我有两个 类 具有多对多关系
@Table(name="employe")
public class Employe implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int Id_emp;
@Column(unique=true)
private String username;
@ManyToMany
@JoinTable(name="produit_employe",joinColumns=@JoinColumn(name="id_employe"),inverseJoinColumns
=@JoinColumn(name="id_produit"))
private List<Produit> produits =new ArrayList<Produit>();
///}
Entity
@Table(name="Produit")
public class Produit implements Serializable {
@Id
private int id_produit;
private String nom_produit;
@ManyToMany(mappedBy="produits")
private List<Employe> employes=new ArrayList<Employe>();
//}
如何显示分配给每位员工的产品名称。 就像我用 sql 做的例子一样,我想把它转换成 hql。
select p.nom_produit from produit_employe pe, produit p where pe.id_produit=p.id_produit AND
pe.id_employe=2
在使用 hql 时,我们必须从实体的角度来思考,而不是像在 sql 中那样。
这相当于 hql 是:
select p.nom_produit from Employe e join e.produits p where e.Id_emp = 2
我已经使用 Spring Data JPA 对其进行了测试,这是员工存储库中使用的代码:
@Query("select p.nom_produit from Employe e join e.produits p where e.Id_emp = :employeId")
List<String> getProductNameAssignedToEmployee (@Param("employeId") Integer employeId);