我在使用 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。