java - MySQL - Hibernate does not create foreign key constraint -
i am, moving application derby mysql, , facing problem hibernate doesn't create foreign key constraints while creating tables. me mappings seems correct working fine derby.
i using mysql server 5.6, , mysql5innodbdialect. verified in mysql workbench using show create table command table being created using innodb engine.
mysql connector used - mysql-connector-java - 5.1.6
logs:
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
as per mysql reference documentation foreign key constraints, foreign key definitions subject following condition:
innodb permits foreign key reference index column or group of columns. however, in referenced table, there must index referenced columns listed first columns in same order.
Comments
Post a Comment