开发者社区> 问答> 正文

Maven hibernate映射文件单独分模块问题:报错

       各位大大,使用Maven开发时hibernate的映射文件和Spring的整合文件在maven的一子模块A中,是jar文件,另一个maven的web模块B读取A的model一直抱notMaping..可是在A模块能单独load出该实体,同样的代码在A中能正常运行,在B中就报org.hibernate.MappingException: Unknown entity

A项目的Spring 配置文件中如下

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
    <context:annotation-config></context:annotation-config>  
     <context:component-scan base-package="com.yolipai"></context:component-scan>
     <!-- 开启CGLIB针对类实现代理 -->
     <aop:aspectj-autoproxy proxy-target-class="true"/>  

    <!-- Spring集成Hibernate -->
   <!-- 1.数据库连接配置(增加了dhcp连接池连接) -->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!-- 2.指定数据库属性配置文件位置-->
    <context:property-placeholder location="classpath*:jdbc.properties"/>
    <!-- 3.配置Hibernate的SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 4.设置Annotations的方式扫描的包 -->
        <property name="packagesToScan">
          <value>com.yolipai</value>
        </property>
         <!-- 扫描配置文件方式 -->
        <!-- <property name="mappingLocations">
          <value>classpath*:com/yolipai/**/*.hbm.xml</value>
        </property> -->
           <!-- Jar包扫描 -->
        <!-- <property name="mappingJarLocations">
            <list>
             <value>WEB-INF/lib/yolipai-core-0.0.1-SNAPSHOT.jar</value>
            </list>
           </property>
           <property name="mappingLocations">
            <list>
             <value>classpath*:com/yolipai/**/*.hbm.xml</value>
            </list>
           </property> -->
        <property name="hibernateProperties">
             <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">=true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
          </props>
        </property>
      </bean>
  <!-- 5.Spring事务配置 -->
  <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
       <aop:config>
        <aop:pointcut id="allMethod"
                expression="execution(* com.yolipai.*..dao.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod"/>
      </aop:config>

  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!-- 6.配置事务加在哪个方法中 -->
      <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
  </tx:advice>
 
  <!-- 开启HibernateTemplate,并且为其注入SessionFactory
    使用HibernateTemplate不太方便的就是要获取session得通过getSessionFactory()方法获取 -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
</beans>





在B项目中spring mvc的配置文件中使用<import resource="classpath*:beans.xml" />引用了A模块的配置文件,出现上述问题,怎么解决?谢谢!

展开
收起
kun坤 2020-06-06 16:35:30 624 0
1 条回答
写回答
取消 提交回答
  • 为什么把映射jar文件的entity配置给注释掉? 打开jar文件的entity映射试试


    ######

    引用来自“兮风古道”的评论

    为什么把映射jar文件的entity配置给注释掉? 打开jar文件的entity映射试试


    楼主说了A能单独load出来  应该没问题,,,我觉得还是没有读取到A的问题
    2020-06-06 16:35:36
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载