0

I have an issue with @Enumerated(EnumType.STRING) enum passed as param to native query after mysql upgrade from 5.7 to 8.0.34.

My simplified setup:

public enum MyEnum {
       A,B;
    }


    @Entity
    @Audited
    @Table(name = "my_table")
    public class MyTableModel {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id", unique = true)
      private Long id;

      @Column(name = "myenum")
      @NotNull
      @Enumerated(EnumType.STRING)
      private MyEnum myEnum;
      .....

The problem arise when I try to update this enum using a native query like this:

Query query = entityManager.createNativeQuery("UPDATE my_table up set  up.myenum=:myenum WHERE up.id=:id ");
        query.setParameter("id", id);
        query.setParameter("myenum", MyEnum.B);
        query.executeUpdate();

I was a working code before in mysql 5.7 pior upgrade to 8.0.34

I tested above with few diffrent hibernate/spring versions: Hibernate 4.2.21 and Spring 4.3.13 gave following error:

Caused by: java.sql.SQLException: Incorrect string value: '\xAC\xED\x00\x05~r...' for column     'myenum' at row 1

Hibernate 5.4.3 and Spring 5.3.27 gave following error:

java.lang.IllegalArgumentException: No enum constant com.example.MyEnum.’~rFcom.example.MyEnumnxrjava.lang.EnumxptA
at java.base/java.lang.Enum.valueOf(Enum.java:240)
at org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor.fromName(EnumJavaTypeDescriptor.java:84)

Am I doing something wrong here with enum? I am not able to update the older version of hibernate/spring and I need to find a way to fix it.

When I change above code to following - it is working as expected - but that would mean I have to change this in a lot of places, why this is a problem upgrading to newer mysql version?

query.setParameter("myenum", MyEnum.B.name())

I verify database/column encoding, change dialect to latest available in given hibernate versions, updated mysql connector to one of the latest one 8.0.15. As above I have tried to manually change enum to plain string using name() and that worked as expected. Is there something missing in the config, model/entity, hibernate etc to continue using just enum instead of plain string in above query?

ArturM
  • 1
  • could you test your nativeQuery with : MyTableModel class like that : Query query = entityManager.createNativeQuery("UPDATE my_table up set up.myenum=:myenum WHERE up.id=:id ",MyTableModel .class); ? – Amine ABBAOUI Aug 16 '23 at 13:41
  • still same issue: Caused by: java.sql.SQLException: Incorrect string value: '\xAC\xED\x00\x05~r...' for column 'myenum' at row 1 Caused by: org.hibernate.exception.GenericJDBCException: could not execute statement org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.GenericJDBCException: could not execute statement; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute statement – ArturM Aug 17 '23 at 08:39
  • and for the MySQL dialect is the correct one ? – Amine ABBAOUI Aug 17 '23 at 13:20
  • yes, I tried for older version dialct mysql, mysql5, mysql57 and i have ported mysql8... for newer version the mysql8 is already there - with same issue each time – ArturM Aug 18 '23 at 07:17

0 Answers0