出现异常: java.lang.IllegalArgumentException: Not an managed type
spring+jpa框架整合出现问题:
从svn checkout项目出来后,把环境搭配好,已经可以把项目在Tomcat中跑起来了
但是在用Junit做单元测试的时候出现问题,一直报错:
java.lang.IllegalStateException: Failed to load ApplicationContext
看完完整的报错信息发现原因是:
Caused by: java.lang.IllegalArgumentException: Not an managed type: class com.csl.mmscm.model.user.Account
原来是jpa找不到实体类,可是我都能在Tomcat中完好跑,照理说应该不用怎么处理了的。
在网上找了两天,终于是发现解决办法了:
原来spring data jpa在applicationContext.xml里面的配置是这样的:
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="packagesToScan" value="com.csl.mmscm.model.*" />
</bean>
这里面引用了persistenceUnit,这个其实就是引用了persistence.xml里面对应的这里面引用了persistenceUnit,原来的persistence.xml配置如下:
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysqlDriver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="!Cascade020" />
<property name="hibernate.connection.url" value="jdbc.mysql://localhost:3306/mmscm"/>
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.show_sql" value="true" />
<!--<property name="hibernate.hbm2ddl.auto" value="create-drop"/>-->
</properties>
</persistence-unit>
</persistence>
在这里面需要使用一个<class>标签来引入jpa所需要的实体类,改为如下:
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<class>com.csl.mmscm.model.user.Account</class>
<class>com.csl.mmscm.model.user.AccountSearchResult</class>
<class>com.csl.mmscm.model.user.LoginSession</class>
<class>com.csl.mmscm.model.user.SysUser</class>
<class>com.csl.mmscm.model.user.SysUserId</class>
<class>com.csl.mmscm.model.user.UserAccessId</class>
<class>com.csl.mmscm.model.user.UserAccessRight</class>
<class>com.csl.mmscm.model.user.UserAlphaSa</class>
<class>com.csl.mmscm.model.user.UserSa</class>
<class>com.csl.mmscm.model.campaign.BatchMms</class>
<class>com.csl.mmscm.model.campaign.BatchMmsId</class>
<class>com.csl.mmscm.model.parameter.AccountParam</class>
<class>com.csl.mmscm.model.parameter.AccountParamId</class>
<class>com.csl.mmscm.model.parameter.SysParam</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysqlDriver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="!Cascade020" />
<property name="hibernate.connection.url" value="jdbc.mysql://localhost:3306/mmscm"/>
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.show_sql" value="true" />
<!--<property name="hibernate.hbm2ddl.auto" value="create-drop"/>-->
</properties>
</persistence-unit>
</persistence>
修改完成后就没有报错了,单元测试成功,可以使用jpa查询到数据库中的数据。