按地图键和&进行过滤使用JPA2的价值

时间:2013-01-14 13:15:17

标签: jpa map

对于定义如下的实体:

@Entity
public class Acl implements Serializable {

@Id
@GeneratedValue
@Column(name = "acl_id")
private Long id;

@ElementCollection(fetch=FetchType.EAGER)
@JoinTable(name = "acl_permits")
private Map<String, Integer> permits = new HashMap<String, Integer>();

查询:

query = em.createQuery("select a FROM Acl a " +
"JOIN a.permits p WITH KEY(p) = '" + user + "' and VALUE(p) = " + permit + "");

转换为:

select acl0_.acl_id as acl1_1_ from Acl acl0_ 
inner join acl_permits permits1_ on acl0_.acl_id=permits1_.Acl_acl_id 
and (permits1_.permits_KEY='user1' 
and (select permits1_.permits from acl_permits permits1_ where acl0_.acl_id=permits1_.Acl_acl_id)=1)

为了将它转换成什么是查询?:

select acl0_.acl_id as acl1_1_ from Acl acl0_ 
inner join acl_permits permits1_ on acl0_.acl_id=permits1_.Acl_acl_id 
and (permits1_.permits_KEY='user1' and acl0_.acl_id=permits1_.Acl_acl_id=1)

1 个答案:

答案 0 :(得分:0)

您可以在JPA中使用本机SQL。所以你可以这样做。

query = em.createNativeQuery("select acl0_.acl_id as acl1_1_ from Acl acl0_ 
    inner join acl_permits permits1_ on acl0_.acl_id=permits1_.Acl_acl_id 
    and (permits1_.permits_KEY=?user and acl0_.acl_id=permits1_.Acl_acl_id=?permit_id)");