【问题标题】:Filter enums on backend using JPA repository使用 JPA 存储库过滤后端枚举
【发布时间】:2019-04-22 18:57:11
【问题描述】:

我有AuditLog 实体,在这个实体中我有@ManyToOne 映射到另一个实体AuditAction,它拥有字段AuditActionType,这是一个枚举。我想使用 JPA 存储库实现后端过滤。我想通过 REST GET 方法返回所有包含 AuditActionTypeAuditLogs 传递给查询参数。 AuditActionType 是:LOG_IN,LOG_OUT,CREATE_USER,UPDATE_USER,DELETE_USER

示例:

http://localhost:8086/audit/action?action=lo

应该返回所有AuditLogs,其中AuditAction 包含“lo”。所以它将返回所有 AuditLogs 和 LOG_INLOG_OUT 动作。

在我的 JPA 存储库中,我创建了这个:

列出 findByAction_actionIgnoreCaseContaining(AuditActionType action);

但是当我运行它时它给了我编译错误:

sed by:java.lang.IllegalStateException:无法忽略 com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType 类型的大小写,属性“操作”必须引用字符串。

有人可以帮忙吗?

审计日志:

package com.cgi.edu.bootcamp.scoringserver.model;

import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "AUDIT_LOG")
public class AuditLog {

    @Id
    @SequenceGenerator(name = "SEQ_audit", sequenceName = "SEQ_audit", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_audit")
    @Column(name = "AL_ID", nullable = false)
    private Long id;

    @ManyToOne
    @JoinColumn(referencedColumnName="U_ID", name="AL_U_ID")
    private User user;

    @ManyToOne
    @JoinColumn(referencedColumnName="AA_ID", name="AL_ACTION")
    private AuditAction action;

    @Column(name = "AL_DESCRIPTION", length = 255)
    private String description;

    @Column(name = "AL_DATE")
    private LocalDateTime date;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public AuditAction getAction() {
        return action;
    }

    public void setAction(AuditAction action) {
        this.action = action;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LocalDateTime getDate() {
        return date;
    }

    public void setDate(LocalDateTime date) {
        this.date = date;
    }
}

审计行动:

    package com.cgi.edu.bootcamp.scoringserver.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType;

@Entity
@Table(name = "AUDIT_ACTION")
public class AuditAction {

    @Id
    @SequenceGenerator(name = "SEQ_action", sequenceName = "SEQ_action", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_action")
    @Column(name = "AA_ID", nullable = false)
    private Long id;

    @Enumerated(EnumType.STRING)
    @NotNull(message = "Audit action can not be empty!")
    @Column(name = "AA_NAME", nullable = false, unique = true)
    private AuditActionType action;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public AuditActionType getAction() {
        return action;
    }

    public void setAction(AuditActionType action) {
        this.action = action;
    }
}

AuditLogRepository:

   package com.cgi.edu.bootcamp.scoringserver.dao;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.cgi.edu.bootcamp.scoringserver.model.AuditLog;
import com.cgi.edu.bootcamp.scoringserver.model.User;
import com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType;

@Repository
public interface AuditLogRepository extends JpaRepository<AuditLog, Long>{

    List<AuditLog> findByAction_actionIgnoreCaseContaining(AuditActionType action);

}

AuditLogServiceImpl:

package com.cgi.edu.bootcamp.scoringserver.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cgi.edu.bootcamp.scoringserver.dao.AuditLogRepository;
import com.cgi.edu.bootcamp.scoringserver.exception.ResourceNotFoundException;
import com.cgi.edu.bootcamp.scoringserver.model.AuditLog;
import com.cgi.edu.bootcamp.scoringserver.model.UserGroup;
import com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType;

@Service
@Transactional
public class AuditLogServiceImpl implements AuditLogService {

    @Autowired
    private AuditLogRepository auditRepository;

    @Override
    public List<AuditLog> findByAction(AuditActionType action) {
        return auditRepository.findByAction_actionIgnoreCaseContaining(action);
    }

}

AuditLogRestController:

    package com.cgi.edu.bootcamp.scoringserver.web;

import java.time.LocalDateTime;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cgi.edu.bootcamp.scoringserver.model.AuditLog;
import com.cgi.edu.bootcamp.scoringserver.model.User;
import com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType;
import com.cgi.edu.bootcamp.scoringserver.service.AuditLogService;

@RestController
@RequestMapping("/audit")
public class AuditLogRestController {

    @Autowired
    private AuditLogService auditLogService;

    @GetMapping("/action")
    public ResponseEntity<List<AuditLog>> getLogsByAction(@RequestParam("action") AuditActionType action){
        return ResponseEntity.ok(auditLogService.findByAction(action));
    }

}

【问题讨论】:

    标签: java spring spring-boot jpa spring-data-jpa


    【解决方案1】:

    将您的存储库方法更改为:

    @Repository
    public interface AuditLogRepository extends JpaRepository<AuditLog, Long>{
    
        List<AuditLog> findByAction(AuditActionType action);
    
    }
    

    如果在控制器方法中映射到枚举值将成功,则无需更改存储库中的案例大小。

    【讨论】:

    • 试过了,http://localhost:8086/audit/action?action=LO 给了我:Failed to convert value of type 'java.lang.String' to required type 'com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType'
    • 因此在控制器中将操作类型更改为字符串并以这种方式使用它 return ResponseEntity.ok(auditLogService.findByAction(AuditActionType.valueOf(action)));或者您可以创建枚举转换器,如此处所述stackoverflow.com/questions/39774427/…
    • 试过 valueOf(),它给了我:java.lang.IllegalArgumentException: No enum constant com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType.Lo
    【解决方案2】:

    好吧,如果您考虑一下,控制器如何将诸如“lo”之类的字符串转换为枚举?因此,您需要首先将参数转换为字符串。

       @GetMapping("/action")
        public ResponseEntity<List<AuditLog>> getLogsByAction(@RequestParam("action") String action){
            return ResponseEntity.ok(auditLogService.findByAction(action));
        }
    

    然后相应地更改服务和存储库方法。

    @Repository
    public interface AuditLogRepository extends JpaRepository<AuditLog, Long>{
    
        @Query("select a from AuditLog a where a.action like CONCAT('%', :action, '%')")
        List<AuditLog> findByAction(String action);
    
    }
    

    【讨论】:

    • 试过了,现在它给了我传递给参数状态200OK和正文空数组的任何字符串:[]
    • 好的,它现在部分工作,我将 a.action 修改为 a.action.action 但它只使用大写字母,当我传递“LOGI”它返回空数组,因为枚举存储在带有下划线“LOG_IN”的数据库中。有什么想法吗?
    • 好的,我用@Query("select a from AuditLog a where a.action.action like UPPER(CONCAT('%', :action, '%'))") 修复了大写问题,但我不知道如何在通过例如“logi”时获得结果。你能帮忙吗?
    猜你喜欢
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    相关资源
    最近更新 更多