簡體   English   中英

Spring 數據 jpa - 錯誤 - 使用枚舉類型參數

[英]Spring data jpa - error - with enum type argument

我對以下代碼有疑問

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController' defined in file [C:\Users\USERNAME\IdeaProjects...\application\ui\security\controller\LoginController.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginServiceImpl' defined in file [C:\Users\USERNAME\IdeaProjects\...\core\domain\login\LoginServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginRepository' defined in be.mypackage.core.persistence.LoginRepository defined in @EnableJpaRepositories declared on DomainConfig: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List be.mypackage.core.persistence.LoginRepository.findListIdentifiant()!

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginServiceImpl' defined in file [C:\Users\USERNAME\IdeaProjects\....\core\domain\login\LoginServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginRepository' defined in be.mypackage.core.persistence.LoginRepository defined in @EnableJpaRepositories declared on DomainConfig: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List be.mypackage.core.persistence.LoginRepository.findListIdentifiant()!

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginRepository' defined in be.mypackage.core.persistence.LoginRepository defined in @EnableJpaRepositories declared on DomainConfig: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List be.mypackage.core.persistence.LoginRepository.findListIdentifiant()!

Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List be.mypackage.core.persistence.LoginRepository.findListIdentifiant()!


Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode 
 \-[IDENT] IdentNode: 'loginRole' {originalText=loginRole}


Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode 
 \-[IDENT] IdentNode: 'loginRole' {originalText=loginRole}

我正在嘗試從表中檢索一些值,但不是全部。 我有這個錯誤。 我不想使用@Transient,因為它會阻止我在其他時間檢索密碼值。

問題似乎出在枚舉字段上,如果我使用適當的構造函數將其從查詢中刪除,則在啟動程序時查詢不再返回錯誤。

一定有一些簡單的事情我做得不對。

我的要求 jpa

import be.mypackage.core.domain.login.Login;

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.jpa.repository.Query;


import java.util.List;



public interface LoginRepository extends JpaRepository<Login, Integer> {

@Query(value = "SELECT idLogin, identifiant, societe, loginRole FROM Login")

List<Login> findListIdentifiant();

}

我的 Class 實體登錄

package be.mypackage.core.domain.login;

import be.mypackage.core.domain.societe.Societe;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Transient;

@Entity
@SQLDelete(sql = "UPDATE login SET status_actif = false WHERE id_login=?")
@Where(clause = "status_actif <> 'false'")
public class Login {

    @Id
    @Column(nullable = false, updatable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer idLogin;

    @Column(length = 100, unique = true)
    private String identifiant;

    //@Transient
    @Column(length = 100)
    private String motDePasse;

    @ManyToOne(fetch = FetchType.LAZY) // OK
    @JoinColumn(name = "no_societe", nullable = false)
    private Societe societe;

    @Transient
    @Column(name = "status_actif", nullable = false)
    private Boolean statusActif;

    @PrePersist
    public void fixStatusActif() {
        this.statusActif = true;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "role", unique = true, length = 30)
    private LoginRole loginRole;

    public Login() {
    }

    // jpa request work with this constructor
    public Login(Integer idLogin, String identifiant, Societe societe) {
        this.idLogin = idLogin;
        this.identifiant = identifiant;
        this.societe = societe;
    }

    // not work
    public Login(Integer idLogin, String identifiant, Societe societe, LoginRole loginRole) {
        this.idLogin = idLogin;
        this.identifiant = identifiant;
        this.societe = societe;
        this.loginRole = loginRole;
    }

    public Integer getIdLogin() {
        return idLogin;
    }

    public void setIdLogin(Integer idLogin) {
        this.idLogin = idLogin;
    }

    public String getIdentifiant() {
        return identifiant;
    }

    public void setIdentifiant(String identifiant) {
        this.identifiant = identifiant;
    }

    public String getMotDePasse() {
        return motDePasse;
    }

    public void setMotDePasse(String motDePasse) {
        this.motDePasse = motDePasse;
    }

    public Boolean getStatusActif() {
        return statusActif;
    }

    public void setStatusActif(Boolean statusActif) {
        this.statusActif = statusActif;
    }

    public Societe getSociete() {
        return societe;
    }

    public void setSociete(Societe societe) {
        this.societe = societe;
    }

    public LoginRole getLoginRole() {
        return loginRole;
    }

    public void setLoginRole(LoginRole loginRole) {
        this.loginRole = loginRole;
    }

    @Override
    public String toString() {
        return identifiant;
    }
}


Class 枚舉

package be.mypackage.core.domain.login;


public enum LoginRole {

    
    ROLE_ADMIN,
    
    ROLE_MANAGER,
    
    ROLE_EMPLOYEE,
    
    ROLE_EMPLOYEE_READ;


}



我的表登錄在此處輸入圖像描述


findAll 查詢適用於枚舉,但我無法從查詢中排除密碼

在此處輸入圖像描述

在此處輸入圖像描述

如您所見,角色(枚舉)字段很好地使用 findAll,但不符合我的安全要求

您不必在查詢中列出屬性:

@Query(value = "SELECT idLogin, identifiant, societe, loginRole FROM Login")
List<Login> findListIdentifiant();

改成:

@Query(value = "SELECT l FROM Login l")
List<Login> findListIdentifiant();

但是這個查詢完全是多余的,因為 JpaRepository 上有一個findAll()方法可以做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM