我在使用 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=false
和 updatable=false
。你应该使用 @MapsId
注解。或者更好的是,忘记复合 ID,而是使用单列、自动生成的 ID。
关于mysql - Hibernate @JoinColumn insertable=false 不起作用(重复的列名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17743190/