2

I would like to create a JPQL query that shows the list of users in the same group as the logged in user.

Indeed, that's what I already did

public List<Utilisateur> getListUser(Long id) {
    return getEntityManager()
            .createQuery("SELECT u FROM Utilisateur u WHERE u.group.id =:" +id)
            .getResultList();
}

and my User tables is as follows

User: id, name, tel, email, group, ...

Group: id, name, type.

My method to retrieve the group of the current user

public Long currentIdGroup() {
    Utilisateur usere = EntityRealm.getUser();
    if (usere == null) {
        return null;
    }
    return EntityRealm.getUser().getGroup().getId();
}

The declaration of the list in the Bean.

public List<Utilisateur> getListusersGroup() {
    return this.utilisateurService.getListUser(currentIdGroup());

}

But, I'm getting this error in glassfish

Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException Exception Description: Syntax error parsing [SELECT u FROM Utilisateur u WHERE u.group.id =:1]. [51, 53] The named input parameter ''{0}'' is not following the rules for a Java identifier.

Thank you in advance for your crucial help.

Many things to you.

1
  • 1
    Remove the colon Commented Jan 24, 2018 at 21:25

1 Answer 1

3

Try removing the colon:

.createQuery("SELECT u FROM Utilisateur u WHERE u.group.id =" +id)

Ideally you should use:

.createQuery("SELECT u FROM Utilisateur u WHERE u.group.id =:param")

and then:

.setParameter("param", id);

to avoid SQL injection.

Sign up to request clarification or add additional context in comments.

2 Comments

in fact I forgot to say that group is a foreign key. Like this User: id, name, tel, email, FK group_id, ... Group: id, name, type. Sorry for that.
Thanks egallardo for your help. It's work. Many thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.