我正在通过hibernate创建一对多映射,但是出现了一些问题,它无法更新表。
栈跟踪
May 05, 2015 8:36:45 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
May 05, 2015 8:36:45 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
May 05, 2015 8:36:45 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Organisation.hbm.xml
May 05, 2015 8:36:45 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
May 05, 2015 8:36:45 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Data.hbm.xml
May 05, 2015 8:36:45 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
May 05, 2015 8:36:45 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
May 05, 2015 8:36:45 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
May 05, 2015 8:36:45 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
May 05, 2015 8:36:45 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
May 05, 2015 8:36:45 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/notifier]
May 05, 2015 8:36:45 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
May 05, 2015 8:36:45 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
May 05, 2015 8:36:45 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
May 05, 2015 8:36:45 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
May 05, 2015 8:36:45 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
May 05, 2015 8:36:45 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
May 05, 2015 8:36:45 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
May 05, 2015 8:36:45 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
May 05, 2015 8:36:45 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: notifier.data
May 05, 2015 8:36:45 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [org_id, id, user_name, address, date, city, mobile_no]
May 05, 2015 8:36:45 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
May 05, 2015 8:36:45 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
May 05, 2015 8:36:45 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: notifier.organisation
May 05, 2015 8:36:45 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [org_id, username, person_mobile_no, email, person_name, estimatedpeople, orgname, org_mobile_no, password, orgaddress, city]
May 05, 2015 8:36:45 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
May 05, 2015 8:36:45 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
May 05, 2015 8:36:46 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000388: Unsuccessful: alter table DATA add index FK1FE7AA2ED256C (ORG_ID), add constraint FK1FE7AA2ED256C foreign key (ORG_ID) references ORGANISATION (ORG_ID)
May 05, 2015 8:36:46 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: Can't create table 'notifier.#sql-b18_c' (errno: 150)
May 05, 2015 8:36:46 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
错误显示在stacktrace的第3行
中Data.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="hibernate.Data" table="DATA">
<id name="id" column="ID">
<generator class="increment" />
</id>
<many-to-one name="" class="hibernate.Organisation">
<column name="ORG_ID"></column>
</many-to-one>
<property name="orgId" column="ORG_ID" insert="true" update="false"
length="10" />
<property name="date" column="DATE" length="20" />
<property name="userName" column="USER_NAME" length="20" />
<property name="mobileNo" column="MOBILE_NO" length="20" />
<property name="address" column="ADDRESS" length="20" />
<property name="city" column="CITY" length="20" />
</class>
Organisation.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="hibernate.Organisation" table="ORGANISATION">
<id name="orgId" column="ORG_ID" length="5">
<generator class="increment" />
</id>
<property name="orgName" column="ORGNAME" length="25" />
<property name="orgAddress" column="ORGADDRESS" length="500" />
<property name="city" column="CITY" length="25" />
<property name="orgMobileNO" column="ORG_MOBILE_NO" length="12" />
<property name="personName" column="PERSON_NAME" length="25" />
<property name="personMobileNo" column="PERSON_MOBILE_NO"
length="12" />
<property name="email" column="EMAIL" length="50" />
<property name="estimatedPeople" column="ESTIMATEDPEOPLE"
length="25" />
<property name="userName" column="USERNAME" />
<property name="password" column="PASSWORD" />
<set name="data" table="DATA" cascade="all">
<!-- used to specify name of foreign key should be created in child table -->
<key column="ORG_ID" />
<one-to-many class="hibernate.Data" />
</set>
</class>
的hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">kartik</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="Organisation.hbm.xml" />
<mapping resource="Data.hbm.xml" />
</session-factory>
</hibernate-configuration>
映射或其他问题是否有问题。
请帮帮我。
提前致谢
答案 0 :(得分:1)
DATA.ORG_ID
和ORGANIZATION.ORG_ID
的数据类型可能不同,请检查。