JPA Query for Enum String type

740 Views Asked by At

I am new to Spring. I have a model named Order which has fields like

id,status,timestamp,customer_id where status is enum type. Model looks like

@Entity
@Table(name="order_") 
public class Order
{
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   public long id;

   @ManyToOne
   @NotNull
   public Customer customer;

   @Temporal(TemporalType.TIMESTAMP)
   @NotNull
   @LastModifiedDate
   @JsonFormat(shape=JsonFormat.Shape.STRING)
   public Date timestamp;

   @OneToMany(fetch=FetchType.EAGER)
   @OrderColumn(nullable = false)
   public List<OrderItem> items = new ArrayList<OrderItem>();

   @Enumerated(EnumType.STRING)
   @NotNull
   public Status status = Status.UNCLAIMED;

   public static enum Status
   {
      UNCLAIMED,
      PICKUP, // pickup on the way
      DELIVERY, // delivery on the way
      DELIVERED,
   }


   public static interface Repository extends PagingAndSortingRepository<Order, Long>
   {
       public List<Order> findTop2ByCustomerIdAndStatus(@Param("customer_id") Long customer_id, @Param("status") String status);

   }
}

When i run the api end point as http://localhost:8080/api/v1/orders/search/findTop2ByCustomerIdAndStatus?customer_id=1&status='UNCLAIMED'

I am getting the response as

{
  "timestamp": 1440744058472,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "org.springframework.dao.InvalidDataAccessApiUsageException",
  "message": "Parameter value [('UNCLAIMED')] did not match expected type [app.models.Order$Status (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [('UNCLAIMED')] did not match expected type [app.models.Order$Status (n/a)]",
  "path": "/api/v1/orders/search/findTop2ByCustomerIdAndStatus"
}

Kindly advise

0

There are 0 best solutions below