Hibernate 查询错误,集合定义为 Set
此错误是从我的 jersey-glassfish 休息端点之一引发的(来自域日志):
原因:java.lang.IllegalArgumentException:为 TypedQuery [com.tanukis.streetama.entity.Flow] 指定的类型与查询返回类型 [接口 java.util.Set] 不兼容 在 org.hibernate.ejb.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:458) 在 com.sun.enterprise.container.common.impl.EntityManagerWrapper.createNamedQuery(EntityManagerWrapper.java:566) 在 com.tanukis.streetama.dao.FluxManager.getBlacklist(FluxManager.java:571)
查询在 orm.xml 中定义:
SELECT DISTINCT s.blacklistedFlow FROM StreetamaUser s WHERE s.uid = :uid
这是我的 StreetamaUser 实体:
@ManyToMany(cascade= javax.persistence.CascadeType.ALL)
@JoinTable(
name="ws_user_blacklist",
uniqueConstraints = @UniqueConstraint(columnNames = { "blacklisted_flow_id", "user_id" }),
joinColumns = {
@JoinColumn(name="user_id",referencedColumnName="uid")
},
inverseJoinColumns = {
@JoinColumn(name="blacklisted_flow_id",referencedColumnName="id")
}
)
@XmlTransient
private Set<Flow> blacklistedFlow = new HashSet<Flow>();
查询调用:
List<Flow> result = em.createNamedQuery( "StreetamaUser.findBlacklist", Flow.class )
.setParameter("iduser", uid )
.setFirstResult(startitem)
.setMaxResults(itemnbr)
.getResultList();
我无法理解 Hibernate 异常。 getResultList 返回一个列表,那么为什么它会抱怨查询返回类型呢?
This error is thrown from one of my jersey-glassfish rest endpoint ( from domain logs ):
Caused by: java.lang.IllegalArgumentException: Type specified for TypedQuery [com.tanukis.streetama.entity.Flow] is incompatible with query return type [interface java.util.Set]
at org.hibernate.ejb.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:458)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createNamedQuery(EntityManagerWrapper.java:566)
at com.tanukis.streetama.dao.FluxManager.getBlacklist(FluxManager.java:571)
The query is defined in orm.xml :
SELECT DISTINCT s.blacklistedFlow FROM StreetamaUser s WHERE s.uid = :uid
Here is my StreetamaUser entity:
@ManyToMany(cascade= javax.persistence.CascadeType.ALL)
@JoinTable(
name="ws_user_blacklist",
uniqueConstraints = @UniqueConstraint(columnNames = { "blacklisted_flow_id", "user_id" }),
joinColumns = {
@JoinColumn(name="user_id",referencedColumnName="uid")
},
inverseJoinColumns = {
@JoinColumn(name="blacklisted_flow_id",referencedColumnName="id")
}
)
@XmlTransient
private Set<Flow> blacklistedFlow = new HashSet<Flow>();
And the query call:
List<Flow> result = em.createNamedQuery( "StreetamaUser.findBlacklist", Flow.class )
.setParameter("iduser", uid )
.setFirstResult(startitem)
.setMaxResults(itemnbr)
.getResultList();
I can't understand the Hibernate exception. getResultList return a list, so why it complains about the query return type ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用 java.Util.List 而不是 Set。
You can try to use a java.Util.List instead of a Set.
你不知道。它始终是一个
List
,即使它不包含像 set 这样的重复值。我一开始就不明白为什么这很重要。You don't. It is always a
List
even if it doesn't contains duplicate values like set. I don't see why it matters in the first place.