0

I am using Spring Boot 2 (2.0.1.RELEASE) with the default Hibernate version with H2 db and I have a problem with the field sizes in the table that is generated when using @Embeddable.

Here is my code:

@Entity
public class Publisher {

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Long id;

  @NotBlank
  @Size(min=2, max=150)
  private String name;

  @NotNull
  private Address address;
  ...
}

@Embeddable
public class Address {

  @NotBlank
  @Size(min=3, max=100)
  private String street;

  @NotBlank
  @Size(min=2, max=100)
  private String city;

  @NotBlank
  @Size(min=2, max=100)
  private String county;

  @NotBlank
  @Size(min=7, max=8)
  private String postcode;
  ...
}

When Spring Boot fires up it creates the tables in H2. The column size in the 'name' attribute in Publisher is correctly set up to 150 using the max value from the @Size annotation.

The max values in the @Size annotations for the embedded Address entity is ignored and set to 255 chars max for all 4 fields.

Is this the expected behaviour or should the db columns from the embedded fields be set to the max value from the @Size annotations in the Address entity?

I know I can set this using the length value of the @Column annotation but I was hoping to get this working without the extra @Column annotation.

Sorry if this is a duplicate but I searched for hours before I decided to post my question here.

Thank you Kaz

Kaz
  • 99
  • 1
  • 1
  • 6

1 Answers1

2

For the recored, the issue is a missing @Valid annotation for the Address field:

@NotNull
@Valid
private Address address;

Without this annotation, cascading is not enabled and the constraint annotations in Address are ignored.

Guillaume Smet
  • 9,921
  • 22
  • 29
  • Hi Guillaume Thank you for your reply. I have raised the issue in Jira: https://hibernate.atlassian.net/browse/HHH-12536 Not sure if all the fields are correct but I have duplicated the above text with the ORM test case template URL in the message. Regards Kaz – Kaz May 01 '18 at 17:35