2014-04-21 92 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的允许外键引用 列的任何索引列或组。但是,在参考表中,必须有一个索引 ,其中被引用的列被列为 相同顺序中的第一列。