Java 集合定义为集合的Hibernate查询错误

Java 集合定义为集合的Hibernate查询错误,java,hibernate,jpa,glassfish,entitymanager,Java,Hibernate,Jpa,Glassfish,Entitymanager,此错误是从我的jersey glassfish rest端点之一(从域日志)引发的: 原因:java.lang.IllegalArgumentException:为TypedQuery[com.tanukis.streetama.entity.Flow]指定的类型与查询返回类型[interface java.util.Set]不兼容 位于org.hibernate.ejb.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManag

此错误是从我的jersey glassfish rest端点之一(从域日志)引发的:

原因:java.lang.IllegalArgumentException:为TypedQuery[com.tanukis.streetama.entity.Flow]指定的类型与查询返回类型[interface java.util.Set]不兼容 位于org.hibernate.ejb.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:458) 位于com.sun.enterprise.container.common.impl.EntityManagerRapper.createNamedQuery(EntityManagerRapper.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>();
@ManyToMany(cascade=javax.persistence.CascadeType.ALL)
@可接合(
name=“ws\u用户\u黑名单”,
uniqueConstraints=@UniqueConstraint(columnNames={“黑名单的\u流\u id”、“用户\u id”}),
joinColumns={
@JoinColumn(name=“user\u id”,referencedColumnName=“uid”)
},
反向连接列={
@JoinColumn(name=“列入黑名单的\u流\u id”,referencedColumnName=“id”)
}
)
@XmlTransient
private Set blacklistedFlow=new HashSet();
和查询调用:

List<Flow> result = em.createNamedQuery( "StreetamaUser.findBlacklist", Flow.class )
            .setParameter("iduser", uid )
            .setFirstResult(startitem)
            .setMaxResults(itemnbr)
            .getResultList();
List result=em.createNamedQuery(“StreetamaUser.findBlacklist”,Flow.class)
.setParameter(“iduser”,uid)
.setFirstResult(startitem)
.setMaxResults(itemnbr)
.getResultList();

我无法理解Hibernate异常。getResultList返回一个列表,那么为什么它会抱怨查询返回类型呢

你没有。它始终是一个
列表
,即使它不包含像set这样的重复值。我一开始不明白这有什么关系。

你没有。它始终是一个
列表
,即使它不包含像set这样的重复值。首先我不明白它为什么重要。

你可以尝试使用java.Util.List而不是Set。

你可以尝试使用java.Util.List而不是Set。

如果你否决了投票,请说出原因。。。谢谢如果你投了反对票,请说出原因。。。谢谢,我知道使用Jackson,您可以使用org.codehaus.Jackson.map.type.TypeReference避免类型擦除。有没有办法做到这一点并避免hibernate异常?我知道对于Jackson,您可以使用org.codehaus.Jackson.map.type.TypeReference避免类型擦除。有没有什么方法可以避免hibernate异常?谢谢你的回答。Hibernate不喜欢列表(包语义)。由于列表中不能包含重复项,所以集合在这里是有意义的。。。但是,是的,如果没有其他解决方案,我可能会更新我的模型并使用列表感谢您的回答。Hibernate不喜欢列表(包语义)。由于列表中不能包含重复项,所以集合在这里是有意义的。。。但是,如果没有其他解决方案,我可以更新我的模型并使用列表