JPA 2.0 CriteriaBuilder:SELECT 来自...
JPA 2.0 CriteriaBuilder : SELECT FROM … IN
我想使用 JPA 2.0 CriteriaBuilder 创建以下 SQL 查询:
SELECT * FROM PROFIL WHERE PROFILID IN (SELECT PROFILID FROM ROLEPROFIL WHERE ROLEID = roleId)
这是我的 类 :
简介:
public class Profil extends AbstractDomain<Long> {
@Id
@Column
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idgen_x")
@UiInfo(name = "Identifiant")
private Long profilId;
@Column(nullable = false)
@UiInfo(name = "Libellé")
private String lib;
}
作用:
public class Role extends AbstractDomain<Long> {
@Id
@Column
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idgen_x")
@UiInfo(name = "Identifiant")
private Long roleId;
}
角色简介:
public class RoleProfil extends AbstractDomain<Long> {
@Id
@Column
@UiInfo(name = "Identifiant")
private Long roleProfilId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
@UiInfo(name = "Profil")
private Profil profilId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
@UiInfo(name = "Rôle")
private Role roleId;
}
我想做的是创建一个函数,使用 JPA 通过 Role
ID 获取 Profils
。
这是我开始的,但我是 JPA 的新手,我不知道该怎么做:
public List<Profil> findProfilsByRoleId(Long roleId) {
final CriteriaBuilder builder = getCriteriaBuilder();
final CriteriaQuery<Profil> criteriaQuery = builder.createQuery(Profil.class);
final Root<Profil> from = criteriaQuery.from(Profil.class);
//TODO
TypedQuery<Profil> query = getEntityManager().createQuery(criteriaQuery);
return query.getResultList();
}
我该怎么做?
SELECT PF.*
FROM PROFIL AS PF
INNER JOIN ROLEPROFIL AS RPF
ON RPF.PROFILID = PF.PROFILID
WHERE RPF.ROLEID = {roleId}
可能是更容易实现的查询。我认为 java 中的代码可能是这样的(我还没有测试刚刚提出的解决方案):
public List<Profil> findProfilsByRoleId(Long roleId) {
final CriteriaBuilder builder = getCriteriaBuilder();
final CriteriaQuery<Profil> query = builder.createQuery(Profil.class);
final Root<RoleProfil> from = query.from(RoleProfil.class);
query.select(from.get(RoleProfil_.profilId));
query.where(builder.equal(from.get(RoleProfil_.roleId).get(Role_.roleId), roleId));
final TypedQuery<Profil> typedQuery = getEntityManager().createQuery(query);
return typedQuery.getResultList();
}
我想使用 JPA 2.0 CriteriaBuilder 创建以下 SQL 查询:
SELECT * FROM PROFIL WHERE PROFILID IN (SELECT PROFILID FROM ROLEPROFIL WHERE ROLEID = roleId)
这是我的 类 :
简介:
public class Profil extends AbstractDomain<Long> {
@Id
@Column
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idgen_x")
@UiInfo(name = "Identifiant")
private Long profilId;
@Column(nullable = false)
@UiInfo(name = "Libellé")
private String lib;
}
作用:
public class Role extends AbstractDomain<Long> {
@Id
@Column
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idgen_x")
@UiInfo(name = "Identifiant")
private Long roleId;
}
角色简介:
public class RoleProfil extends AbstractDomain<Long> {
@Id
@Column
@UiInfo(name = "Identifiant")
private Long roleProfilId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
@UiInfo(name = "Profil")
private Profil profilId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
@UiInfo(name = "Rôle")
private Role roleId;
}
我想做的是创建一个函数,使用 JPA 通过 Role
ID 获取 Profils
。
这是我开始的,但我是 JPA 的新手,我不知道该怎么做:
public List<Profil> findProfilsByRoleId(Long roleId) {
final CriteriaBuilder builder = getCriteriaBuilder();
final CriteriaQuery<Profil> criteriaQuery = builder.createQuery(Profil.class);
final Root<Profil> from = criteriaQuery.from(Profil.class);
//TODO
TypedQuery<Profil> query = getEntityManager().createQuery(criteriaQuery);
return query.getResultList();
}
我该怎么做?
SELECT PF.*
FROM PROFIL AS PF
INNER JOIN ROLEPROFIL AS RPF
ON RPF.PROFILID = PF.PROFILID
WHERE RPF.ROLEID = {roleId}
可能是更容易实现的查询。我认为 java 中的代码可能是这样的(我还没有测试刚刚提出的解决方案):
public List<Profil> findProfilsByRoleId(Long roleId) {
final CriteriaBuilder builder = getCriteriaBuilder();
final CriteriaQuery<Profil> query = builder.createQuery(Profil.class);
final Root<RoleProfil> from = query.from(RoleProfil.class);
query.select(from.get(RoleProfil_.profilId));
query.where(builder.equal(from.get(RoleProfil_.roleId).get(Role_.roleId), roleId));
final TypedQuery<Profil> typedQuery = getEntityManager().createQuery(query);
return typedQuery.getResultList();
}