【问题标题】:Hibernate @JoinColumn insertable=false doesn't work (duplicate column name)Hibernate @JoinColumn insertable=false 不起作用(列名重复)
【发布时间】:2013-07-19 09:54:55
【问题描述】:

我在使用 hibernate hdd2auto 创建一些表时遇到问题。使用组合外键时,有必要指出在更新/创建表时应忽略作为键的一部分的字段上的 @ManyToOne 关系。 即使我把这些设置在日志中的结果如下:

2013-07-19 11:44:11 ERROR [hibernate.tool.hbm2ddl.SchemaUpdate:235] - HHH000388: Unsuccessful: create table `ActionLocale` (`ActionId` integer not null, `LanguageId` integer not null, `ActionName` varchar(255), ActionId integer, LanguageId integer, primary key (`ActionId`, `LanguageId`)) ENGINE=InnoDB
2013-07-19 11:44:11 ERROR [hibernate.tool.hbm2ddl.SchemaUpdate:236] - Duplicate column name 'ActionId'

如您所见,ActionId 列在查询中被声明了两次!为什么??

这些是我的课。

可嵌入密钥:

@Embeddable
public class ActionLocalePK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="ActionId")
    private int actionId;

    @Column(name="LanguageId")
    private int languageId;

类表:

@Entity
public class ActionLocale implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private ActionLocalePK id;

    @Column(name="ActionName")
    private String actionName;

    //bi-directional many-to-one association to Action
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="ActionId",insertable=false,updatable=false)
    private Action action;

    //bi-directional many-to-one association to Language
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="LanguageId",insertable=false,updatable=false)
    private Language language;

在我设置的hibernate.cfg.xml中:

<property name="hibernate.connection.CharSet">utf8</property>
        <property name="hibernate.connection.characterEncoding">utf8</property>
        <property name="hibernate.connection.useUnicode">true</property>

        <property name="hibernate.globally_quoted_identifiers">true</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

        <!-- Update the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

我在 Ubuntu 上使用 MySQL Ver 14.14 Distrib 5.5.31。休眠版本是 4.1。

【问题讨论】:

    标签: mysql hibernate jpa


    【解决方案1】:

    您不应该使用insertable=falseupdatable=false。您应该使用 @MapsId 注释。或者更好的是,忘记复合 ID,而是使用单列自动生成的 ID。

    【讨论】:

    • 你能解释一下为什么我们不应该使用 insertable 和 updatable 吗?你能帮忙吗:stackoverflow.com/questions/25771892/…
    • 因为将关联映射到 ID 列的记录方法是使用 MapsId。
    【解决方案2】:

    insertable=false 和 updatable=false 将在保存记录时忽略。首先,您应该手动创建表,然后使用它。 (或)尝试使用@transient(未测试)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 2021-09-14
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      相关资源
      最近更新 更多