Hibernate"无法解析来自资源的映射文档"

时间:2012-12-03 12:34:04

标签: hibernate grails

我的第一个grails应用程序出了问题。我试图修改我的hibernate文件一百次,但我仍然得到这个例外:

    | Error 2012-12-03 13:24:23,190 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error executing bootstraps:
 Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org
.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve
 reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframewor
k.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed;
 nested exception is org.hibernate.InvalidMappingException: Could not parse mapping document from resource user.hbm.xml
Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exceptio
n is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot
 resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.spring
framework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method
 failed; nested exception is org.hibernate.InvalidMappingException: Could not parse mapping document from resource user.
hbm.xml
    Line | Method
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 's
essionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanC
reationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is 
org.hibernate.InvalidMappingException: Could not parse mapping document from resource user.hbm.xml
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; neste
d exception is org.hibernate.InvalidMappingException: Could not parse mapping document from resource user.hbm.xml
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . in java.lang.Thread
Caused by InvalidMappingException: Could not parse mapping document from resource user.hbm.xml
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . in java.lang.Thread
Caused by DuplicateMappingException: Duplicate class/entity mapping santrain.User
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . in java.lang.Thread

User.groovy:

package test

class User {

    Integer id
    String f1
    String f2

    static constraints = {
    }
}

的hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    '-//Hibernate/Hibernate Configuration DTD 3.0//EN'
    'http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd'>

<hibernate-configuration>

    <session-factory>
        <mapping resource='user.hbm.xml'/>
    </session-factory>

</hibernate-configuration>

编辑:我写的时候 - &gt;一切正常,所以错误必须在user.hbm.xml ...

的任何地方

user.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test">
    <class name="test.User" table="test_user">
        <id name="id" column="id" type="java.lang.Integer" />
        <property column="f1" name="f1" type="java.lang.String" />
        <property column="f2" name="f2" type="java.lang.String" />
    </class>
</hibernate-mapping>

DataSource.groovy中

dataSource {
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    username = "testuser"
    password = "testpass"
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "update" 
            url = "jdbc:mysql://localhost:3306/testdb?autoreconnect=true"
        }
    }
...

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

我删除了所有的hibernate-mappings并使用了     static mapping = {table'sgroovy_user'} 感谢Ian Roberts!