Filter enums on backend using JPA repository
I have AuditLog
entity, in this entity I have @ManyToOne
mapping to another entity AuditAction
that owns field AuditActionType
which is an enum. I want implement backend filtering using JPA repository. I want to return by REST GET method all AuditLog
s that contains AuditActionType
passed to query param.
AuditActionType
are: LOG_IN
,LOG_OUT
,CREATE_USER
,UPDATE_USER
,DELETE_USER
Example:
http://localhost:8086/audit/action?action=lo
should return all AuditLog
s where their AuditAction
contains 'lo'. So It would return all AuditLog
s with LOG_IN
and LOG_OUT
action.
In my JPA repository I created this:
List findByAction_actionIgnoreCaseContaining(AuditActionType action);
but when I run this It gives me compilation error:
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.
Please can somebody help?
AuditLog:
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));
}
}
java spring spring-boot jpa spring-data-jpa
add a comment |
I have AuditLog
entity, in this entity I have @ManyToOne
mapping to another entity AuditAction
that owns field AuditActionType
which is an enum. I want implement backend filtering using JPA repository. I want to return by REST GET method all AuditLog
s that contains AuditActionType
passed to query param.
AuditActionType
are: LOG_IN
,LOG_OUT
,CREATE_USER
,UPDATE_USER
,DELETE_USER
Example:
http://localhost:8086/audit/action?action=lo
should return all AuditLog
s where their AuditAction
contains 'lo'. So It would return all AuditLog
s with LOG_IN
and LOG_OUT
action.
In my JPA repository I created this:
List findByAction_actionIgnoreCaseContaining(AuditActionType action);
but when I run this It gives me compilation error:
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.
Please can somebody help?
AuditLog:
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));
}
}
java spring spring-boot jpa spring-data-jpa
add a comment |
I have AuditLog
entity, in this entity I have @ManyToOne
mapping to another entity AuditAction
that owns field AuditActionType
which is an enum. I want implement backend filtering using JPA repository. I want to return by REST GET method all AuditLog
s that contains AuditActionType
passed to query param.
AuditActionType
are: LOG_IN
,LOG_OUT
,CREATE_USER
,UPDATE_USER
,DELETE_USER
Example:
http://localhost:8086/audit/action?action=lo
should return all AuditLog
s where their AuditAction
contains 'lo'. So It would return all AuditLog
s with LOG_IN
and LOG_OUT
action.
In my JPA repository I created this:
List findByAction_actionIgnoreCaseContaining(AuditActionType action);
but when I run this It gives me compilation error:
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.
Please can somebody help?
AuditLog:
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));
}
}
java spring spring-boot jpa spring-data-jpa
I have AuditLog
entity, in this entity I have @ManyToOne
mapping to another entity AuditAction
that owns field AuditActionType
which is an enum. I want implement backend filtering using JPA repository. I want to return by REST GET method all AuditLog
s that contains AuditActionType
passed to query param.
AuditActionType
are: LOG_IN
,LOG_OUT
,CREATE_USER
,UPDATE_USER
,DELETE_USER
Example:
http://localhost:8086/audit/action?action=lo
should return all AuditLog
s where their AuditAction
contains 'lo'. So It would return all AuditLog
s with LOG_IN
and LOG_OUT
action.
In my JPA repository I created this:
List findByAction_actionIgnoreCaseContaining(AuditActionType action);
but when I run this It gives me compilation error:
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.
Please can somebody help?
AuditLog:
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));
}
}
java spring spring-boot jpa spring-data-jpa
java spring spring-boot jpa spring-data-jpa
edited Nov 20 at 11:28
Lorelorelore
1,85061124
1,85061124
asked Nov 20 at 10:39
Michael
226
226
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Well, if you think about it, how an the the controller convert a String such as 'lo' to enum? So you'd need to start by converting the parameter to a String.
@GetMapping("/action")
public ResponseEntity<List<AuditLog>> getLogsByAction(@RequestParam("action") String action){
return ResponseEntity.ok(auditLogService.findByAction(action));
}
and then change the service and repository methods accordingly.
@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);
}
tried that, now it gives me on whatever string I pass to params status200OK
and in Body empty array:
– Michael
Nov 20 at 18:33
Ok, its working partially now, I modified a.action to a.action.action but its working only with uppercase letter and when I pass "LOGI" it returns empty array because of that enum is stored in db with underscore "LOG_IN". Any ideas?
– Michael
Nov 21 at 12:40
ok, I fixed the uppercase problem with@Query("select a from AuditLog a where a.action.action like UPPER(CONCAT('%', :action, '%'))")
but I dont know how to manage to get result when I pass for example "logi". Can you help?
– Michael
Nov 21 at 13:15
add a comment |
Change your repository method to:
@Repository
public interface AuditLogRepository extends JpaRepository<AuditLog, Long>{
List<AuditLog> findByAction(AuditActionType action);
}
If mapping to enum value in your controller method will be successful, you don't need to change case size in the repository.
tried that,http://localhost:8086/audit/action?action=LO
gives me :Failed to convert value of type 'java.lang.String' to required type 'com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType'
– Michael
Nov 20 at 11:51
So in controller change action type to String and use it this way return ResponseEntity.ok(auditLogService.findByAction(AuditActionType.valueOf(action))); Or you can create enum converter as it is described here stackoverflow.com/questions/39774427/…
– Centos
Nov 20 at 12:10
tried valueOf(), it gives me:java.lang.IllegalArgumentException: No enum constant com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType.Lo
– Michael
Nov 20 at 18:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391181%2ffilter-enums-on-backend-using-jpa-repository%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Well, if you think about it, how an the the controller convert a String such as 'lo' to enum? So you'd need to start by converting the parameter to a String.
@GetMapping("/action")
public ResponseEntity<List<AuditLog>> getLogsByAction(@RequestParam("action") String action){
return ResponseEntity.ok(auditLogService.findByAction(action));
}
and then change the service and repository methods accordingly.
@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);
}
tried that, now it gives me on whatever string I pass to params status200OK
and in Body empty array:
– Michael
Nov 20 at 18:33
Ok, its working partially now, I modified a.action to a.action.action but its working only with uppercase letter and when I pass "LOGI" it returns empty array because of that enum is stored in db with underscore "LOG_IN". Any ideas?
– Michael
Nov 21 at 12:40
ok, I fixed the uppercase problem with@Query("select a from AuditLog a where a.action.action like UPPER(CONCAT('%', :action, '%'))")
but I dont know how to manage to get result when I pass for example "logi". Can you help?
– Michael
Nov 21 at 13:15
add a comment |
Well, if you think about it, how an the the controller convert a String such as 'lo' to enum? So you'd need to start by converting the parameter to a String.
@GetMapping("/action")
public ResponseEntity<List<AuditLog>> getLogsByAction(@RequestParam("action") String action){
return ResponseEntity.ok(auditLogService.findByAction(action));
}
and then change the service and repository methods accordingly.
@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);
}
tried that, now it gives me on whatever string I pass to params status200OK
and in Body empty array:
– Michael
Nov 20 at 18:33
Ok, its working partially now, I modified a.action to a.action.action but its working only with uppercase letter and when I pass "LOGI" it returns empty array because of that enum is stored in db with underscore "LOG_IN". Any ideas?
– Michael
Nov 21 at 12:40
ok, I fixed the uppercase problem with@Query("select a from AuditLog a where a.action.action like UPPER(CONCAT('%', :action, '%'))")
but I dont know how to manage to get result when I pass for example "logi". Can you help?
– Michael
Nov 21 at 13:15
add a comment |
Well, if you think about it, how an the the controller convert a String such as 'lo' to enum? So you'd need to start by converting the parameter to a String.
@GetMapping("/action")
public ResponseEntity<List<AuditLog>> getLogsByAction(@RequestParam("action") String action){
return ResponseEntity.ok(auditLogService.findByAction(action));
}
and then change the service and repository methods accordingly.
@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);
}
Well, if you think about it, how an the the controller convert a String such as 'lo' to enum? So you'd need to start by converting the parameter to a String.
@GetMapping("/action")
public ResponseEntity<List<AuditLog>> getLogsByAction(@RequestParam("action") String action){
return ResponseEntity.ok(auditLogService.findByAction(action));
}
and then change the service and repository methods accordingly.
@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);
}
edited Nov 20 at 14:15
answered Nov 20 at 13:25
Alan Hay
15.2k22768
15.2k22768
tried that, now it gives me on whatever string I pass to params status200OK
and in Body empty array:
– Michael
Nov 20 at 18:33
Ok, its working partially now, I modified a.action to a.action.action but its working only with uppercase letter and when I pass "LOGI" it returns empty array because of that enum is stored in db with underscore "LOG_IN". Any ideas?
– Michael
Nov 21 at 12:40
ok, I fixed the uppercase problem with@Query("select a from AuditLog a where a.action.action like UPPER(CONCAT('%', :action, '%'))")
but I dont know how to manage to get result when I pass for example "logi". Can you help?
– Michael
Nov 21 at 13:15
add a comment |
tried that, now it gives me on whatever string I pass to params status200OK
and in Body empty array:
– Michael
Nov 20 at 18:33
Ok, its working partially now, I modified a.action to a.action.action but its working only with uppercase letter and when I pass "LOGI" it returns empty array because of that enum is stored in db with underscore "LOG_IN". Any ideas?
– Michael
Nov 21 at 12:40
ok, I fixed the uppercase problem with@Query("select a from AuditLog a where a.action.action like UPPER(CONCAT('%', :action, '%'))")
but I dont know how to manage to get result when I pass for example "logi". Can you help?
– Michael
Nov 21 at 13:15
tried that, now it gives me on whatever string I pass to params status
200OK
and in Body empty array:
– Michael
Nov 20 at 18:33
tried that, now it gives me on whatever string I pass to params status
200OK
and in Body empty array:
– Michael
Nov 20 at 18:33
Ok, its working partially now, I modified a.action to a.action.action but its working only with uppercase letter and when I pass "LOGI" it returns empty array because of that enum is stored in db with underscore "LOG_IN". Any ideas?
– Michael
Nov 21 at 12:40
Ok, its working partially now, I modified a.action to a.action.action but its working only with uppercase letter and when I pass "LOGI" it returns empty array because of that enum is stored in db with underscore "LOG_IN". Any ideas?
– Michael
Nov 21 at 12:40
ok, I fixed the uppercase problem with
@Query("select a from AuditLog a where a.action.action like UPPER(CONCAT('%', :action, '%'))")
but I dont know how to manage to get result when I pass for example "logi". Can you help?– Michael
Nov 21 at 13:15
ok, I fixed the uppercase problem with
@Query("select a from AuditLog a where a.action.action like UPPER(CONCAT('%', :action, '%'))")
but I dont know how to manage to get result when I pass for example "logi". Can you help?– Michael
Nov 21 at 13:15
add a comment |
Change your repository method to:
@Repository
public interface AuditLogRepository extends JpaRepository<AuditLog, Long>{
List<AuditLog> findByAction(AuditActionType action);
}
If mapping to enum value in your controller method will be successful, you don't need to change case size in the repository.
tried that,http://localhost:8086/audit/action?action=LO
gives me :Failed to convert value of type 'java.lang.String' to required type 'com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType'
– Michael
Nov 20 at 11:51
So in controller change action type to String and use it this way return ResponseEntity.ok(auditLogService.findByAction(AuditActionType.valueOf(action))); Or you can create enum converter as it is described here stackoverflow.com/questions/39774427/…
– Centos
Nov 20 at 12:10
tried valueOf(), it gives me:java.lang.IllegalArgumentException: No enum constant com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType.Lo
– Michael
Nov 20 at 18:42
add a comment |
Change your repository method to:
@Repository
public interface AuditLogRepository extends JpaRepository<AuditLog, Long>{
List<AuditLog> findByAction(AuditActionType action);
}
If mapping to enum value in your controller method will be successful, you don't need to change case size in the repository.
tried that,http://localhost:8086/audit/action?action=LO
gives me :Failed to convert value of type 'java.lang.String' to required type 'com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType'
– Michael
Nov 20 at 11:51
So in controller change action type to String and use it this way return ResponseEntity.ok(auditLogService.findByAction(AuditActionType.valueOf(action))); Or you can create enum converter as it is described here stackoverflow.com/questions/39774427/…
– Centos
Nov 20 at 12:10
tried valueOf(), it gives me:java.lang.IllegalArgumentException: No enum constant com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType.Lo
– Michael
Nov 20 at 18:42
add a comment |
Change your repository method to:
@Repository
public interface AuditLogRepository extends JpaRepository<AuditLog, Long>{
List<AuditLog> findByAction(AuditActionType action);
}
If mapping to enum value in your controller method will be successful, you don't need to change case size in the repository.
Change your repository method to:
@Repository
public interface AuditLogRepository extends JpaRepository<AuditLog, Long>{
List<AuditLog> findByAction(AuditActionType action);
}
If mapping to enum value in your controller method will be successful, you don't need to change case size in the repository.
answered Nov 20 at 10:53
Centos
19019
19019
tried that,http://localhost:8086/audit/action?action=LO
gives me :Failed to convert value of type 'java.lang.String' to required type 'com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType'
– Michael
Nov 20 at 11:51
So in controller change action type to String and use it this way return ResponseEntity.ok(auditLogService.findByAction(AuditActionType.valueOf(action))); Or you can create enum converter as it is described here stackoverflow.com/questions/39774427/…
– Centos
Nov 20 at 12:10
tried valueOf(), it gives me:java.lang.IllegalArgumentException: No enum constant com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType.Lo
– Michael
Nov 20 at 18:42
add a comment |
tried that,http://localhost:8086/audit/action?action=LO
gives me :Failed to convert value of type 'java.lang.String' to required type 'com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType'
– Michael
Nov 20 at 11:51
So in controller change action type to String and use it this way return ResponseEntity.ok(auditLogService.findByAction(AuditActionType.valueOf(action))); Or you can create enum converter as it is described here stackoverflow.com/questions/39774427/…
– Centos
Nov 20 at 12:10
tried valueOf(), it gives me:java.lang.IllegalArgumentException: No enum constant com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType.Lo
– Michael
Nov 20 at 18:42
tried that,
http://localhost:8086/audit/action?action=LO
gives me : Failed to convert value of type 'java.lang.String' to required type 'com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType'
– Michael
Nov 20 at 11:51
tried that,
http://localhost:8086/audit/action?action=LO
gives me : Failed to convert value of type 'java.lang.String' to required type 'com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType'
– Michael
Nov 20 at 11:51
So in controller change action type to String and use it this way return ResponseEntity.ok(auditLogService.findByAction(AuditActionType.valueOf(action))); Or you can create enum converter as it is described here stackoverflow.com/questions/39774427/…
– Centos
Nov 20 at 12:10
So in controller change action type to String and use it this way return ResponseEntity.ok(auditLogService.findByAction(AuditActionType.valueOf(action))); Or you can create enum converter as it is described here stackoverflow.com/questions/39774427/…
– Centos
Nov 20 at 12:10
tried valueOf(), it gives me:
java.lang.IllegalArgumentException: No enum constant com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType.Lo
– Michael
Nov 20 at 18:42
tried valueOf(), it gives me:
java.lang.IllegalArgumentException: No enum constant com.cgi.edu.bootcamp.scoringserver.model.enums.AuditActionType.Lo
– Michael
Nov 20 at 18:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391181%2ffilter-enums-on-backend-using-jpa-repository%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown