【问题标题】:Hibernate : unable to map mapping document as they are already available inHibernate:无法映射映射文档,因为它们已经在
【发布时间】:2016-09-27 09:24:01
【问题描述】:

configuration.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_xml</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="format_sql">true</property>

        <!-- mapping configurations -->

        <mapping resource="resources/Employee.hbm.xml" />
        <mapping resource="resources/Department.hbm.xml" />

    </session-factory>
</hibernate-configuration>

这是我的Employee.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.domain.Employee" table="employee">
        <id name="id" type="long" column="id">
            <generator class="increment" />
        </id>
        <property name="firstName" name="firstName" />
        <property name="salary" name="salary" />

        <many-to-one name="department" class="com.domain.Department">
            <column name="department" />
        </many-to-one>
    </class>
</hibernate-mapping>

department.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.domain.Department" table="department">
        <id name="id" type="long" column="id">
            <generator class="auto" />
        </id>
        <property name="deptName" column="deptName" />
    </class>
</hibernate-mapping>

会话工厂

public static SessionFactory getSessionFactory() {
        SessionFactory sessionFactory = null;
        try {
            sessionFactory = new Configuration().configure("resources/configuration.cfg.xml")
                    .addResource("resources/Employee.hbm.xml").addResource("resources/Department.hbm.xml")
                    .buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
        return sessionFactory;
    }

最后是例外

线程“主”java.lang.ExceptionInInitializerError 中的异常 com.utility.HibernateUtil.getSessionFactory(HibernateUtil.java:15) 在 com.client.Client.getTransaction(Client.java:13) 在 com.client.Client.main(Client.java:32) 原因: org.hibernate.boot.InvalidMappingException:无法解析映射 文件:resources/Employee.hbm.xml (RESOURCE) 在 org.hibernate.boot.jaxb.in​​ternal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:46) 在 org.hibernate.boot.jaxb.in​​ternal.UrlXmlSource.doBind(UrlXmlSource.java:36) 在 org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:59) 在 org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274) 在 org.hibernate.cfg.Configuration.addResource(Configuration.java:498) 在 com.utility.HibernateUtil.getSessionFactory(HibernateUtil.java:12) ... 2 更多原因:org.hibernate.boot.MappingException:无法 在第 0 行和第 0 列执行解组。消息:null: 来源(资源/Employee.hbm.xml)在 org.hibernate.boot.jaxb.in​​ternal.AbstractBinder.jaxb(AbstractBinder.java:177) 在 org.hibernate.boot.jaxb.in​​ternal.MappingBinder.doBind(MappingBinder.java:61) 在 org.hibernate.boot.jaxb.in​​ternal.AbstractBinder.doBind(AbstractBinder.java:102) 在 org.hibernate.boot.jaxb.in​​ternal.AbstractBinder.bind(AbstractBinder.java:57) 在 org.hibernate.boot.jaxb.in​​ternal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:43) ... 7 更多原因:javax.xml.bind.UnmarshalException - 带有链接异常:[javax.xml.stream.XMLStreamException: ParseError at [row,col]:[10,49] 消息: http://www.w3.org/TR/1999/REC-xml-names-19990114#AttributeNotUnique?property&name] 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:470) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:448) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:420) 在 org.hibernate.boot.jaxb.in​​ternal.AbstractBinder.jaxb(AbstractBinder.java:171) ... 11 更多原因:javax.xml.stream.XMLStreamException: ParseError at [row,col]:[10,49] 消息: http://www.w3.org/TR/1999/REC-xml-names-19990114#AttributeNotUnique?property&name 在 com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:596) 在 com.sun.xml.internal.stream.XMLEventReaderImpl.peek(XMLEventReaderImpl.java:276) 在 javax.xml.stream.util.EventReaderDelegate.peek(EventReaderDelegate.java:104) 在 org.hibernate.boot.jaxb.in​​ternal.stax.BufferedXMLEventReader.peek(BufferedXMLEventReader.java:96) 在 javax.xml.stream.util.EventReaderDelegate.peek(EventReaderDelegate.java:104) 在 org.hibernate.boot.jaxb.in​​ternal.stax.HbmEventReader.peek(HbmEventReader.java:47) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.handleCharacters(StAXEventConnector.java:164) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(StAXEventConnector.java:126) 在 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:445) ... 13 更多

解决该错误后,我遇到了其他错误

Hibernate: 
    alter table employee 
        drop 
        foreign key FKkxx4wtsgsdt16iix2pso0k126
Sep 27, 2016 3:23:37 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
WARN: GenerationTarget encountered exception accepting command : Unable to execute command [
    alter table employee 
        drop 
        foreign key FKkxx4wtsgsdt16iix2pso0k126]
org.hibernate.tool.schema.spi.CommandAcceptanceException: Unable to execute command [
    alter table employee 
        drop 
        foreign key FKkxx4wtsgsdt16iix2pso0k126]
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:63)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:370)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:355)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applyConstraintDropping(SchemaDropperImpl.java:327)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:229)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:153)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:125)
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:111)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:137)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:65)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:308)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:483)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:707)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:723)
    at com.utility.HibernateUtil.getSessionFactory(HibernateUtil.java:13)
    at com.client.Client.getTransaction(Client.java:13)
    at com.client.Client.main(Client.java:32)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'hibernate_xml.employee' doesn't exist
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
    at com.mysql.jdbc.Statement.execute(Statement.java:727)
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:51)
    ... 16 more

Hibernate: 
    drop table if exists department
Hibernate: 
    drop table if exists employee
Hibernate: 
    create table department (
        id bigint not null,
        deptName varchar(255),
        primary key (id)
    )
Hibernate: 
    create table employee (
        id bigint not null,
        firstName varchar(255),
        salary double precision,
        department bigint,
        primary key (id)
    )
Hibernate: 
    alter table employee 
        add constraint FKkxx4wtsgsdt16iix2pso0k126 
        foreign key (department) 
        references department (id)
Sep 27, 2016 3:23:38 PM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@bd06ca'
department saved with id : 0
Hibernate: 
    select
        max(id) 
    from
        employee
employee saved with id 1
Sep 27, 2016 3:23:39 PM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance beforeQuery flushing: com.domain.Department]
Exception in thread "main" java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance beforeQuery flushing: com.domain.Department
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:144)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1403)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:473)
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3133)
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2370)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
    at com.client.Client.getTransaction(Client.java:27)
    at com.client.Client.main(Client.java:32)
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance beforeQuery flushing: com.domain.Department
    at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:279)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:462)
    at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:298)
    at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:309)
    at org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:296)
    at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:4139)
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:528)
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:215)
    at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:142)
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:216)
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:85)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:38)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1397)
    ... 10 more

如果一切就绪,我不确定为什么会出现此错误?

请帮忙

【问题讨论】:

    标签: java xml hibernate


    【解决方案1】:

    Employee.hbm.xml 中的name 属性已经指定(使用两次/重复),请使用Employee.hbm.xml 中的以下更新代码:

        <property name="firstName" column="firstName"/>
        <property name="salary" column="firstName" />
    

    文件名应该是Employee.hbm.xml而不是Employee.xml,因为它是一个映射文件。

    您需要为第二个异常使用以下代码:

         <property name="hbm2ddl.auto">update</property>
    

    【讨论】:

    • 感谢您的评论,如果您不介意,它会导致我出现其他错误...
    • 好的,请用另一个错误更新您的问题。
    • 删除之前创建的所有表并运行新的代码副本。更改员工的架构似乎存在问题。还要添加用于在db中保存数据的代码。
    • 我在 one-to-one 映射中丢失了 cascade="all",现在它正在工作,也感谢您的时间 :)
    猜你喜欢
    • 2011-02-14
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多