【问题标题】:Hibernate not creating correct fields for Clobs in MySQLHibernate 没有为 MySQL 中的 Clob 创建正确的字段
【发布时间】:2011-01-08 16:45:06
【问题描述】:

我的实体中有一个字符串

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

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

【问题讨论】:

    标签: hibernate annotations


    【解决方案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 方法中使用注解配置时效果很好。

    问候,

    【讨论】:

    • "只是一个建议:如果可能的话,将注解配置放在 getter 方法而不是成员字段中。Hibernate 使用代理来完成您的工作。在 getter 方法中使用注解配置时效果很好。"是什么伎俩。我在属性而不是吸气剂上有注释。谢谢!
    猜你喜欢
    • 2016-10-25
    • 2015-07-07
    • 2017-07-22
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多