Hibernate没有为MySQL中的Clobs创建正确的字段

时间:2010-01-24 02:11:21

标签: hibernate annotations

我的实体中有一个字符串

@Column(length=40000)
@Lob
private String introText;

我的MySQL数据库中Hibernate为introText创建的列是一个varchar(255),它不会与列长度或@Lob注释一起生成。有任何想法吗?我对Hibernate比较陌生,所以我想知道我是否缺少任何其他设置或配置。

1 个答案:

答案 0 :(得分:1)

执行以下操作后

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

在脚本中,我看到了

IntroText TEXT

所以它没有按预期工作。所以我的建议是:使用columnDefinition属性代替

  

它允许您定义用于定义列类型的精确DDL

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

现在它运作正常!您可以测试是否需要

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);

只是一个建议:如果可能,将注释配置放在getter方法而不是成员字段中。 Hibernate使用Proxies来完成你的工作。在getter方法中使用注释配置时,它可以正常工作。

的问候,