2

I searched this but unable to correct my query. Need help on this. Here is my 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

}

Here is my LmsRole class

@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;
    }
    ...
}

Here is my LmsRoleLmsFeature class

@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;
    }
    ...
}

Now here is the query that I am trying to make but getting exception

@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);

I am getting following exception

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

I tried joins but I am unable to make it. I tried different settings but unable to resolve it. Please help.

Thanks

Basit
  • 8,426
  • 46
  • 116
  • 196

1 Answers1

0

vu360user0_.id.lmsRoles is a Collection. As such, it does not have an attribute named lmsRoleLmsFeature.

You can't do a join u.lmsRoles.lmsRoleLmsFeature lrlf with a Collection (lmsRoles) among the expression, but you can split this in a new JOIN operation:

join u.lmsRoles lmsRoles
join lmsRoles.lmsRoleLmsFeature lrlf

So, do you need to rewrite youy JPQL, like this:

@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);

And be careful with this amount of JOIN FETCH. This will generated a big query with a lot of duplicated information in the result.

Community
  • 1
  • 1
Dherik
  • 17,757
  • 11
  • 115
  • 164