Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlExceptionHelper.logExceptions Bad value for type int : WRAP

Tags:

java

spring

Hi I was following spring in Action 5th edition book and I have problem when I want to get parameters from Ingredient table. In the following method I am trying to fill up list using 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";
}

but catсh 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 how my Ingredient entity looks:

@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 can I fix it? Thanks in advance

like image 319
Art3 Avatar asked May 16 '26 00:05

Art3


1 Answers

Enums with JPA can use different fetch methods with the @Enumerated annotation.

It seems that by default the values are obtained by ordinal values, that's why your get this error, since you are storing the values as string .

Try the following, to tell JPA that your enum values are persisted as String :

@Column(columnDefinition = "varchar")
@Enumerated(EnumType.STRING)
private final Type type;
like image 199
Arnaud Avatar answered May 18 '26 14:05

Arnaud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!