使用JPA信息库在后端过滤枚举

时间:2018-11-20 10:39:50

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

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

示例:

  

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

应返回其DELETE_USER包含“ lo”的所有AuditLog。因此它将返回具有AuditActionAuditLog动作的所有LOG_IN

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

  

列出findByAction_actionIgnoreCaseContaining(AuditActionType action);

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

  

作者:java.lang.IllegalStateException:无法忽略com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType类型的情况,属性“ action”必须引用字符串。

请有人帮忙吗?

AuditLog:

LOG_OUT

审核动作:

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

AuditLogRepository:

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

AuditLogServiceImpl:

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

}

AuditLogRestController:

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

}

2 个答案:

答案 0 :(得分:1)

好吧,如果您考虑一下,控制器如何将字符串(例如“ 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);

}

答案 1 :(得分:0)

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

Rectangle

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