2014-04-21 94 views
0

我是,將我的應用程序從Derby移動到MySQL,並面臨hibernate在初始創建表時並未創建外鍵約束的問題。我所有的映射似乎都是正確的,因爲他們與德比一起工作得很好。MySQL - Hibernate不會創建外鍵約束

我正在使用MySQL服務器5.6和MySQL5InnoDBDialect。另外我在MySQL工作臺中使用show create table命令驗證了該表是使用innoDB引擎創建的。

MySQL的連接器中使用 - MySQL的連接器的Java - 5.1.6

日誌:

2014-04-21 12:38:29,820 ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: alter table DEPENDENCY_TRACK add constraint FK25E3373737E62D9A foreign key (TOOL_ID) references TOOL_MASTER 
2014-04-21 12:38:29,820 ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Cannot add foreign key constraint 
2014-04-21 12:38:30,127 ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: alter table DEPT_ROLE add constraint FK7E895E3080D96D8 foreign key (DEPARTMENT_ID) references DEPARTMENT 
2014-04-21 12:38:30,128 ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Cannot add foreign key constraint 

Persistance.xml

<?xml version="1.0" encoding="UTF-8"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> 
    <persistence-unit name="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL"> 
     <properties> 
      <property name="hibernate.hbm2ddl.auto" value="create" /> 
      <!-- <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" /> --> 
      <!-- <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" /> --> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> 
      <property name="hibernate.show_sql" value="true"/> 
      <property key="hibernate.hbm2ddl.import_files">import.sql</property> 
     </properties> 
     <mapping-file>META-INF/orm.xml</mapping-file> 
    </persistence-unit> 
</persistence> 

database.properties

# database properties 
app.jdbc.driverClassName = com.mysql.jdbc.Driver 
app.jdbc.url    = jdbc:mysql://localhost:3306/alt 
app.jdbc.username   = root 
app.jdbc.password   = root 
+0

您可以提供DEPENDENCY_TRACK,TOOL_MASTER和DEPT_ROLE,DEPARTMENT表及其alter查詢的import.sql剪輯。 –

+0

import.sql只包含插入查詢。我不是通過它明確創建表。 –

回答

0

根據MySQL參考erence documentation for FOREIGN KEY Constraints,外鍵定義是符合以下條件:

InnoDB的允許外鍵引用 列的任何索引列或組。但是,在參考表中,必須有一個索引 ,其中被引用的列被列爲 相同順序中的第一列。