1

I am using Spring MVC with Maven and I have a structure of modules a below.

The Web Project is MVC Module I moved the models which I was using in Web Module to the Model Module. It compiles and runs just ok.

But when i try to use the entity manager I get an Unknown Entity Error.

Is there a configuration which I am missing to add for my entity manager configuration to access the model classes in the Model Module?

I added the dependencies for the Model Module in the Web Module and I can access the classes in the Model module. But the entity manager looks like just cant access the model module.

 main
 |
 --Web
 |
 --Model

jpaContext.xml:

<context:annotation-config />
<context:component-scan base-package="com.myproject"/>


<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="punit"/>
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
        </bean>
    </property>

    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <entry key="hibernate.hbm2ddl.auto" value="create" />
            <entry key="hibernate.format_sql" value="true" />
        </map>
    </property>

</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/Web?autoReconnect=true" />
    <property name="username" value="root" />
    <property name="password" value="123456" />
</bean>

POM.XML FOR MODEL MODULE

<modelVersion>4.0.0</modelVersion>
 <parent>
   <groupId>com.myproject</groupId>
  <artifactId>main</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Model</artifactId>
<dependencies>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.2.0.Final</version>
</dependency>
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0.2</version>
</dependency>

 </dependencies>

POM.XML FOR WEB MODULE

        <modelVersion>4.0.0</modelVersion>
<artifactId>Web</artifactId>
<packaging>war</packaging>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.21</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.1.9.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.myproject</groupId>
        <artifactId>Model</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
<parent>
    <groupId>com.myproject</groupId>
    <artifactId>main</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
4

2 回答 2

1

尝试

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

    <property name="packagesToScan">com.myproject.model</property>

    ... other properties ...

</bean>
于 2013-09-28T00:03:15.447 回答
0

问题在于,在我的案例中,实体管理器配置文件 jpaContext.xml 位于我的 WEbModule/Resources 文件夹中,并且 Web 模块依赖于模型模块以使用模型模块中定义的实体类。

默认情况下具有@Entity 注解的模型模块中的实体会搜索实体管理器,并且因为实体管理器是在 Web 模块中定义的,所以我无法访问它,这就是为什么我收到未找到实体错误的原因。在我的情况下,因为我的 Web 模块已经引用了模型模块,由于循环依赖,我不能将模型模块引用到 Web 项目。

The soultion was to move my jpaContext.xml to Model Module and Works Just Fine
于 2013-09-30T20:32:10.703 回答