我有AuditLog
实体,在这个实体中,我有@ManyToOne
映射到另一个拥有字段AuditAction
的实体AuditActionType
,该字段是一个枚举。我想使用JPA储存库实施后端过滤。我想通过REST GET方法返回所有包含传递给查询参数的AuditLog
的AuditActionType
。AuditActionType
是:LOG_IN
,LOG_OUT
,CREATE_USER
,UPDATE_USER
,DELETE_USER
例:
http://localhost:8086/audit/action?action=lo
应该返回其AuditLog
包含“ lo”的所有AuditAction
。因此它将通过AuditLog
和LOG_IN
操作返回所有LOG_OUT
。
在我的JPA存储库中,我创建了以下代码:
列出findByAction_actionIgnoreCaseContaining(AuditActionType action);
但是当我运行它时,它给了我编译错误:
sed by:java.lang.IllegalStateException:无法忽略com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType类型的情况,属性“ action”必须引用一个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;
}
}
AuditAction:
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));
}
}
最佳答案
好吧,如果您考虑一下,控制器如何将字符串(例如“ 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);
}