简体   繁体   中英

JPQL query using a DiscriminatorColumn in Spring JpaRepository results in QuerySyntaxException: xxx is not mapped

I would like to select users that have role as CLIENT but I got this error :

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: utilisateur is not mapped [SELECT u FROM utilisateur u WHERE u.role=:CLIENT] at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:219) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:143) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] ...

Utilisateur.java:

// imports omitted for brevity
    @Entity
    @Table(name="UTILISATEURS")
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name="ROLE")
    public class Utilisateur implements Serializable 
    {
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
           private long codeUtilisateur;
           private String cin;
           private String nom;
           private String prenom;
           private String username;
           private String password;
           private String telephone;
           private String ville;
           private Boolean actif;
           private String adresse;
        @DateTimeFormat(pattern = "dd/MM/yyyy")
           private Date dateNaissance;
           private String email;
           private char genre;

           public Utilisateur() {
            super();
        }

        public Utilisateur(long code) {
            this.codeUtilisateur = code;
        }



        public Utilisateur(String cin, String nom, String prenom, String telephone, String email, char genre, Boolean actif) {
            super();
            this.cin = cin;
            this.nom = nom;
            this.prenom = prenom;
            this.telephone = telephone;
            this.email = email;
            this.genre = genre;
            this.actif = actif;
        }

        public Utilisateur(String cin, String nom, String prenom, String username, String password, String telephone,
                String ville, String adresse, Date dateNaissance, String email, char genre, Boolean actif) {
            super();
            this.cin = cin;
            this.nom = nom;
            this.prenom = prenom;
            this.username = username;
            this.password = password;
            this.telephone = telephone;
            this.ville = ville;
            this.adresse = adresse;
            this.dateNaissance = dateNaissance;
            this.email = email;
            this.genre = genre;
            this.actif = actif;
        }
// getters/setters omitted for brevity
    }

UtilisateurRepository.java:

// imports omitted for brevity

    public interface UtilisateurRepository extends JpaRepository <Utilisateur,Long> {
        @Query ("SELECT u FROM utilisateurs u WHERE u.role=:CLIENT")
           public List<Client> getClients();
    }

I see several issues with your Repository.

  1. You are using the table name instead of the Entity name in your JPQL query.
  2. You are attempting to select a List of Utilisateur objects, but the signature of getClients() returns List<Client> instead of List<Utilisateur> .
  3. You are attempting to match the role to a substituted value ( :CLIENT ) that is not part of your getClients() signature.

You are configuring Utilisateur as a parent using the following annotations:

@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="ROLE")

Therefore, I am assuming you have at least one sub-class of Utilisateur , Client , which you are attempting to query for:

@Entity
public class Client extends Utilisateur {...}

In which case, try the following:

public interface UtilisateurRepository extends JpaRepository <Utilisateur,Long> {
    @Query ("SELECT u FROM Utilisateur u WHERE TYPE(u) = Client")
    public List<Utilisateur> getClients();
}

Otherwise, you may wish to have a separate ClientRepository:

public interface ClientRepository extends JpaRepository<Client,Long> {
    // in which case you can just take advantage of findAll
    // which is already provided by JpaRepository
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM