2012-05-24 142 views
3

我正在学习春季休眠zk堆栈,并做我的第一次crud后this tutorial 我把applicationContext.xml到webapp/WEB-INF和.hbm.xml资源/映射 但我不知道为什么我的HBM文件保持显示无法找到我的pojos。放置applicationcontext.xml和.hbm文件的位置?

在github上

https://github.com/kossel/firstzk

我有这样的结构

enter image description here

的applicationContext.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <!-- set other Hibernate properties in hibernate.cfg.xml file --> 
     <property name="configLocation" value="classpath:/com/iknition/firstzk/hibernate/hibernate.cfg.xml" /> 
    </bean> 

的hibernate.cfg.xml

<mapping resource="com/iknition/firstzk/hibernate/Company.hbm.xml" /> 
    <mapping resource="com/iknition/firstzk/hibernate/Contact.hbm.xml" /> 

Company.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 package="com.iknition.firstzk.beans"> 
    <class name="Contact" table="contact"> 
     <id name="idcontact" column="idcontact" type="integer"> 
      <generator class="increment" /> 
     </id> 
     <property name="name" column="name" type="string" /> 
     <property name="email" column="email" type="string" /> 
     <many-to-one name="company" column="companyId" class="com.iknition.firstzk.beans.Company" outer-join="true" /> 
    </class> 
</hibernate-mapping> 

Contact.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 package="com.iknition.firstzk.beans"> 
    <class name="Contact" table="contact"> 
     <id name="idcontact" column="idcontact" type="integer"> 
      <generator class="increment" /> 
     </id> 
     <property name="name" column="name" type="string" /> 
     <property name="email" column="email" type="string" /> 
     <many-to-one name="company" column="companyId" class="com.iknition.firstzk.beans.Company" outer-join="true" /> 
    </class> 
</hibernate-mapping> 

UPDATE:

  • 我有参考contact.hbm.xml过,我错过了把它放在这里。
  • 通过“为什么我的HBM文件一直显示无法找到我的POJO”我的意思是,当我尝试构建应用程序,我不断收到的“Caused by: org.hibernate.MappingException: entity class not found: com.iknition.firstzk.beans.Contact”我已经改变了很多次错误的配置文件的位置,并仍然有相同的错误。
+0

您可以扩展此:“*为什么我的HBM文件继续显示找不到我的pojos *“?你遇到什么问题?你也只是在'hibernate.cfg.xml'中引用'Company.hbm.xml'。那么Contact.hbm.xml呢? –

+0

对不起,我已经更新了这个问题,以澄清你问的问题。 – Kossel

回答

1

我认为你必须指定mappingLocations一个接一个,如:

<property name="mappingLocations"> 
    <util:list> 
    <value>hibernate/Company.hbm.xml</value> 
    <value>hibernate/Contact.hbm.xml</value> 
    </util:list> 
</property> 
+0

试过注释和工作,然后我改变你的建议,并且工作了! :D – Kossel

+0

好的。查看LocalSessionFactoryBean的文档。它需要一个Resource []的数组。而且你不能用* .hbm.xml链接它们。 –

+0

注释是不正确的,因为你已经在pom.xml中为Java 1.4配置了项目 –

2

嗯......从未尝试过使用外部hibernate.cfg.xml。但我想指定只加载属性。您可能仍然需要在单独的属性中设置映射。

这里是我的配置通常是这样的:

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource"> 
     <ref bean="dataSource" /> 
    </property> 
    <property name="hibernateProperties"> 
     <bean 
      class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
      <property name="propertiesArray"> 
       <list> 
        <props>...</props> 
       </list> 
      </property> 
     </bean> 
    </property> 
    <property name="mappingLocations" value="classpath:com/iknition/firstzk/hibernate/*.hbm.xml"/> 

</bean> 
+0

我一直在玩位置和配置applicationContext文件,我仍然有同样的错误。我已经更新了这个问题。我怀疑问题是在我的.hbm配置 – Kossel

+0

后来我试过这种方式,也有效:S – Kossel