2010-01-24 34 views
1

没有创造CLOBS正确的领域我有一个字符串在我的实体的Hibernate在MySQL

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

,Hibernate对创建introText在我的MySQL数据库的列是VARCHAR(255),它不与摇摆列长度​​或@Lob注释。有任何想法吗?我对Hibernate相对来说比较新,所以我想知道我是否缺少任何其他设置或配置。

回答

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使用代理来完成你的工作。在getter方法中使用注释配置时,它工作正常。

问候,

+0

“只是一个建议:如果可能的话,把注释配置getter方法,而不是成员领域Hibernate使用代理服务器做你的工作,它的getter方法使用注释配置时工作得很好。”是什么伎俩。我在属性上有注释,而不是吸气剂。 谢谢! – lupefiasco 2010-02-02 23:46:26