mysql - Hibernate @JoinColumn insertable=false 不起作用(重复的列名)

标签 mysql hibernate jpa

我在使用 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 列在查询中声明了两次!为什么??

这些是我的类(class)。

可嵌入 key :

@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。 Hibernate 版本是 4.1。

最佳答案

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

关于mysql - Hibernate @JoinColumn insertable=false 不起作用(重复的列名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17743190/

相关文章:

php - 创建 "custom"用户 URL 时出现问题

java - JPQL 连接( hibernate )

Hibernate 与 CFQuery

sql - 一对多 多对一映射,服务器试图找到一个不存在的表

caching - Eclipselink缓存问题(一个数据库两个系统)

mysql - 我无法弄清楚该SQL查询中的错误在哪里

php - php函数也会影响子目录吗?

mysql - 表是否可能大于文件系统的最大文件大小?

java - Hibernate:Criteria API:查询 CollectionOfElements 中不包含指定元素的实体?

java - 如何对实体强制进行版本更新?