Java – Error value of type SqlExceptionHelper.logExceptions int: WRAP

Error value of type SqlExceptionHelper.logExceptions int: WRAP… here is a solution to the problem.

Error value of type SqlExceptionHelper.logExceptions int: WRAP

Hello, I followed Spring in the 5th edition of the Action book and I had a problem when I wanted to get the parameters from the Ingredient table. In the following method, I tried populating the list with ingredientRepository.findAll():

@GetMapping
public String showDesignForm(Model model){
    List<Ingredient> ingredients = new ArrayList<>();
    ingredientRepository.findAll().forEach(ingredients::add);

Type[] types = Ingredient.Type.values();
    for (Type type : types){
        model.addAttribute(type.toString().toLowerCase(),
                filterByType(ingredients, type));
    }
    model.addAttribute("taco", new Taco());
    return "welcomePage";
}

However, I encountered the following error:

org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute query; SQL [select ingredient0_.id as id1_0_, ingredient0_.name as name2_0_, ingredient0_.type as type3_0_ from Ingredient ingredient0_]; nested exception is org.hibernate.exception.DataException: could not execute query] with root cause
    org.postgresql.util.PSQLException: Bad value for type int : WRAP

This is what my constituent entity looks like:

@Data
@RequiredArgsConstructor
@NoArgsConstructor(access=AccessLevel.PRIVATE, force=true)
@Entity
public class Ingredient {
    @Id
    @Column(name = "id", columnDefinition = "varchar(4)")
    private final String id;
    @Column(columnDefinition = "varchar(25)")
    private final String name;
    @Column(columnDefinition = "varchar")
    private final Type type;
    public static enum Type {
        WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
    }
}

How do I fix it? Thanks in advance

Solution

Enumerations with JPA can be obtained using different methods using @Enumerated annotations.

It seems that by default, values are obtained by ordinal values, which is why this error occurs because you store the value as a string.

Try the following to tell JPA that your enumeration value is persisted as a String:

@Column(columnDefinition = "varchar")
@Enumerated(EnumType.STRING)
private final Type type;

Related Problems and Solutions