Hibernate String column - found [character (Types#CHAR)], but expecting [char (Types#VARCHAR)]

287 views Asked by At

So I have a String attribute about a code which is exactly 4 characters long: "1A9C".

Therefor I chose CHAR(4) as a datatype on the database (DB2 z/OS or in-mem H2).

My entity looks like this:

@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Table(
    name = "FOO",
    uniqueConstraints = {@UniqueConstraint(columnNames = {"CODE", "ABC"})})
@IdClass(FooId.class)
/*default*/ class Foo {

  @Id
  @Column(name = "CODE", columnDefinition = "char", length = 4, nullable = false)
  @Type(type = "String")
  private String code;

  @Id
  @Column(name = "ABC", columnDefinition = "char", length = 3, nullable = false)
  @Type(type = "String")
  private String abc;

  @Column(name = "STATUS", columnDefinition = "smallint", nullable = false)
  @Type(type = "org.hibernate.type.NumericBooleanType")
  private boolean status;
}

JPA repo:

@Repository
/*default*/ interface FooRepository
    extends JpaRepository<Foo, FooId>, JpaSpecificationExecutor<Foo> {}

And my test:

final Foo newFoo = new Foo("A1BC", "133", true);
final Foo savedFoo = repo.saveAndFlush(newFoo);

Fails due to this error:

java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Character (java.lang.String and java.lang.Character are in module java.base of loader 'bootstrap')

EDIT

It also does not work if I select @Type(type = "char") for the fields on the @Entity

EDIT 2

Without the @Type annotation I got

SchemaManagementException: Schema-validation: wrong column type encountered in column [code] in table [foo]; found [character (Types#CHAR)], but expecting [char (Types#VARCHAR)]

EDIT 3

Seems like this is related to it https://hibernate.atlassian.net/browse/HHH-14831

0

There are 0 answers