获取错误非法尝试取消引用元素属性为[lmsRoleLmsFeature]的集合[vu360user0_.id.lmsRoles]的引用

问题描述 投票:2回答:2

我搜索了此内容,但无法更正我的查询。需要帮助。这是我的VU360User

@Entity
public class VU360User extends AuditedEntity implements UserDetails, CredentialsContainer, Cloneable {
    ...
    private Learner learner;
    private Set<LmsRole> lmsRoles;
    ...

    @OneToOne(mappedBy = "vu360User", fetch=FetchType.LAZY)
    public Learner getLearner() {
        return learner;
    }
    //Setter

    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(
        name = "VU360USER_ROLE", 
        joinColumns = { 
            @JoinColumn(name = "USER_ID", referencedColumnName = "ID") 
        }, 
        inverseJoinColumns = { 
            @JoinColumn(name = "ROLE_ID", referencedColumnName = "ID") 
        }
    )
    public Set<LmsRole> getLmsRoles() {
        return lmsRoles;
    }
    //Setter

}

这是我的LmsRole类

@Entity
public class LmsRole extends BaseEntity implements GrantedAuthority  {
    ....
    private List<LmsRoleLmsFeature> lmsRoleLmsFeature;

    @OneToMany(mappedBy = "lmsRole", fetch=FetchType.LAZY)
    public List<LmsRoleLmsFeature> getLmsRoleLmsFeature() {
        return lmsRoleLmsFeature;
    }

    public void setLmsRoleLmsFeature(List<LmsRoleLmsFeature> lmsRoleLmsFeature) {
        this.lmsRoleLmsFeature = lmsRoleLmsFeature;
    }
    ...
}

这是我的LmsRoleLmsFeature类

@Entity
public class LmsRoleLmsFeature extends BaseEntity implements Serializable{
    ...
    private LmsRole lmsRole;
    private LmsFeature lmsFeature;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="LMSROLE_ID")
    public LmsRole getLmsRole() {
        return lmsRole;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="LMSFEATURE_ID")
    public LmsFeature getLmsFeature() {
        return lmsFeature;
    }
    ...
}

现在是我要尝试但出现异常的查询

@Query("select distinct u, l, c, d, ta, la, p, ra, i from #{#entityName} u, IN (u.lmsRoles) lr "
        + "join fetch u.learner l "
        + "join fetch l.customer c "
        + "join fetch c.distributor d "
        + "join fetch u.trainingAdministrator ta "
        + "join fetch u.lmsAdministrator la "
        + "join fetch u.proctor p "
        + "join fetch u.regulatoryAnalyst ra "
        + "join fetch u.instructor i "
        + "join u.lmsRoles.lmsRoleLmsFeature lrlf "
        //+ "join lr.lmsRoleLmsFeature lrlf "
        + "where u.username = :userName and c.activeTf = true and d.status = true")
VU360User findByUserNameWithAllEntitiesAssociations(@Param("userName")String userName);

我正在追随例外

Caused by: org.hibernate.QueryException: illegal attempt to dereference collection [vu360user0_.id.lmsRoles] with element property reference [lmsRoleLmsFeature]

我尝试过加入,但无法成功。我尝试了其他设置,但无法解决。请帮助。

谢谢

hql jpql jpa-2.1 hibernate-5.x
2个回答
0
投票

vu360user0_.id.lmsRoles是一个集合。因此,it does not have an attribute名为lmsRoleLmsFeature

您不能对表达式中的集合(join u.lmsRoles.lmsRoleLmsFeature lrlf)进行lmsRoles,但可以在新的JOIN操作中将其拆分:

join u.lmsRoles lmsRoles
join lmsRoles.lmsRoleLmsFeature lrlf

所以,您需要像这样重写您的JPQL:

@Query("select distinct u, l, c, d, ta, la, p, ra, i from #{#entityName} u, IN (u.lmsRoles) lr "
        + "join fetch u.learner l "
        + "join fetch l.customer c "
        + "join fetch c.distributor d "
        + "join fetch u.trainingAdministrator ta "
        + "join fetch u.lmsAdministrator la "
        + "join fetch u.proctor p "
        + "join fetch u.regulatoryAnalyst ra "
        + "join fetch u.instructor i "
        + "join fetch u.instructor i "
        + "join u.lmsRoles lmsRoles"
        + "join lmsRoles.lmsRoleLmsFeature lrlf"
        + "where u.username = :userName and c.activeTf = true and d.status = true")
VU360User findByUserNameWithAllEntitiesAssociations(@Param("userName")String userName);

并且请注意此JOIN FETCH的数量。这将生成一个大查询,结果中包含很多重复的信息。


0
投票

我有同样的例外情况“非法尝试取消对收藏的引用”。首先,我创建一个查询,该查询返回not emty结果,然后添加一个条件,例如,我使用具有oneToMany关系的集合,但是不幸的是,当我使用join来获取结果时,我获得了“非法尝试取消引用集合”的例证。设置为空!您能给我另一个解决方案以在请求中使用我的收藏吗?谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.