开发者

Hibernate not creating correct fields for Clobs in MySQL

开发者 https://www.devze.com 2022-12-17 03:44 出处:网络
I have a String in my Entity @Column(length=40000) @Lob 开发者_运维技巧private String introText; The column in my MySQL database that Hibernate creates for introText is a varchar(255), which doesn\

I have a String in my Entity

@Column(length=40000)
@Lob
开发者_运维技巧private String introText;

The column in my MySQL database that Hibernate creates for introText is a varchar(255), which doesn't jive with the column length or @Lob annotation. Any ideas? I'm relatively new to Hibernate, so I wonder if I'm missing any other settings or configurations.


After doing the following

// Notice without @Lob
@Column(length=4000)
private String getIntroText() {
    return this.introText;
}

In script, i see

IntroText TEXT

So it does not work as expected. So my advice is: use columnDefinition attribute instead

It allows you to define the exact DDL used to define the column type

@Lob
@Column(columnDefinition="TEXT (4000)")
private String getIntroText() {
    return this.introText;
}

Now it works fine! You can test if you want

AnnotationConfiguration configuration = new AnnotationConfiguration();

configuration
    .addAnnotatedClass(<YOUR_ENTITY_GOES_HERE>.class)
    .setProperty(Environment.HBM2DDL_AUTO, "create")
    .setProperty(Environment.USER, "<USER_GOES_HERE>")
    .setProperty(Environment.PASS, "<USER_PASS_GOES_HERE>")
    .setProperty(Environment.SHOW_SQL, "true")
    .setProperty(Environment.FORMAT_SQL, "true")
    // Set up your dialect according to the Target MySQL
    .setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQLDialect")
    .setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver")
    .setProperty(Environment.URL, "jdbc:mysql://127.0.0.1:3306/<YOUR_SCHEMA_GOES_HERE>");

SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile("schema.sql");

schema.create(true, true);

Just an advice: if possible, put annotation configuration in getter method instead of member field. Hibernate uses Proxies to do your job. And it works fine when using annotation configuration in getter method.

regards,

0

精彩评论

暂无评论...
验证码 换一张
取 消