java - 使用 JPA 存储库在后端过滤枚举

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

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

示例:

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

应返回 AuditAction 包含“lo”的所有 AuditLog。因此,它将返回带有 LOG_INLOG_OUT 操作的所有 AuditLog

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

List findByAction_actionIgnoreCaseContaining(AuditActionType action);

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

sed by: java.lang.IllegalStateException: Unable to ignore case of com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType types, the property 'action' must reference a String.

请问有人可以帮忙吗?

审核日志:

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

}

最佳答案

好吧,如果你想一下, Controller 如何将“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);

}

关于java - 使用 JPA 存储库在后端过滤枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53391181/

相关文章:

java - 带有 id 和 activity_main 的 kotlin 中未解析的引用

java - 异常 : The AXIS engine could not find a target service to invoke! targetService 是 SecurityDepositServiceImpl

java - SpringBoot 2.1.3 : Embedded Tomcat Logging

java - Spring Boot : How to Disable JNDI lookup and use spring. 数据源代替测试?

java - 在 JList 上添加 JScrollPane 而不使用 JPanel

Java,在其他地方使用数组中的字符串?

java - 我不断使用公共(public)数组获取 NullPointerException

java - 将字符串与函数连接起来以获取 JSON 输出

spring - 从Spring Boot Rest Controller 访问JWT token

java - 两个不同数据库中同时对两个表进行事务