1

Having a following property in my entity:

@Setter
@Size(min = 3, max = 255)
@Column(length = 500)
private String xo;

Generated column in h2 database is

CHARACTER VARYING(255)

I expected it to be 500. Of course I can change max/length values but shouldn't only Column annotation be taken into account when determining database column length?

Update:
I will provide class (in this case max Size is set to 400 and it will result with CHARACTER VARYING(400) on db side), properties file and pom.xml which I use.
Once the app is up and running you need to go to http://localhost:8080/h2-console insert jdbc:h2:mem:testdb url and password into form and click "Connect".

package org.example;

import lombok.Setter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Size;

@SpringBootApplication
public class App
{
    public static void main(String[] args)
    {
        SpringApplication.run(App.class, args);
    }
}

@Setter
@Entity
class SampleClass
{
    @Id
    @GeneratedValue
    Long id;

    @Size(min = 3, max = 400)
    @Column(length = 500)
    private String xo;
}
# application.properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.jpa.defer-datasource-initialization=true

spring.h2.console.enabled=true
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>col_length_test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
    </dependencies>
</project>
1

1 Answer 1

-1

No, the length of a generated column is directly affected only by the @Column annotation through its length attribute. The @Size annotation is used for validation purposes in the application layer and does not affect the column definition in the database schema.

3
  • Did you misread the post? If it's only affected by the @Column annotation, as you say, then why is it 255? Commented Apr 13 at 14:37
  • Could you please provide additional details, including the versions of the dependencies you are using, the complete code for entity, and the JPA properties you have set up ? Commented Apr 13 at 17:01
  • @OussamaBASRY sure, please see snippets which I added in original post.
    – bridgemnc
    Commented Apr 14 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.