当前位置: 首页 > 知识库问答 >
问题:

尚未注入实体管理器

白侯林
2023-03-14

我刚刚开始使用Spring ROO,并使用数据库逆向工程命令生成了我的实体类。然而每当我试图调用生成的实体类中的一个CRUD方法时,我总是得到这个异常:Java . lang . illegalstateexception:实体管理器没有被注入(Spring Aspects JAR是否被配置为AJC/AJDT方面库?)

我怀疑(通过查看生成的文件)EntityManager没有被注入到类中。你能告诉我我缺少什么配置吗?

这是我applicationContext.xml的样子

    <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<context:spring-configured/>
<context:component-scan base-package="com.lennartz">
    <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="${database.driverClassName}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
    <property name="numTestsPerEvictionRun" value="3"/>
    <property name="minEvictableIdleTimeMillis" value="1800000"/>
    <property name="validationQuery" value="SELECT 1 FROM DUAL"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

和生成的实体文件

privileged aspect UserDetail_Roo_Jpa_ActiveRecord {

@PersistenceContext
transient EntityManager UserDetail.entityManager;

public static final EntityManager UserDetail.entityManager() {
    EntityManager em = new UserDetail().entityManager;
    if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
    return em;
}

如果我错过了什么,请告诉我。

共有2个答案

巩光誉
2023-03-14

我最终找到了问题,似乎上下文没有在我的应用程序中初始化。我在我的网络中添加以下行.xml并且它起作用了

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

如果您的应用程序不是 Web 应用程序,我认为使用 ClassPathXmlApplicationContext 初始化上下文应该适合您。

沈德寿
2023-03-14

您不能修改.aj文件,以自定义Roo生成的代码读取http://docs.spring.io/spring-roo/docs/2.0.0.M1/reference/html/#edit-修改和自定义roo生成的代码

 类似资料:
  • 我有一个EAR应用程序(要部署在WebLogic12c上),它有一个“持久化”组件。“persist”组件使用JPA(实现:EclipseLink)来持久化对象。 使用entityManager的bean声明为,实体管理器通过注释注入。 问题是,我每次尝试访问entityManager时都有一个(意思是他没有被正确注入)。 persistence.xml 我在常规代码中是这样使用的: MyBean

  • 我有一个在Eclipse中定义的非常基本的JavaEE应用程序(一个EJB,一个带有PrimeFaces的网页,一个JPA实体)。它有四个项目,一个用于EAR,一个用于EJB,一个用于JPA,一个用于Web部件。问题是,我试图在JPA项目的persistence.xml中配置HiberNate作为持久化框架,但它并没有像我预期的那样完全工作。在我的EJB(@Statless)中,我可以通过两种方式

  • 我一直在研究很多类似的问题,这些问题并没有反映我的确切问题。如果我忽略了有人已经解决了这个问题,请让我知道。 我目前正在将JBoss 3.x上的旧EJB2.1应用程序迁移到JBoss 7.x上EJB3.x。由于应用程序客户端和服务器之间通信的变化,我创建了一个小的testclient来检查缺陷。 其中之一是我的无状态会话bean中的entitymanager没有被注入,使entitymanager

  • 问题内容: 一个很长的问题,请忍受我。 我们将Spring + JPA用于Web应用程序。我的团队在讨论如何在注入的(基于泛型的东西对AppFuse中提供的线DAO,我们不使用过的注射某种原因)。我们正在使用“应用程序管理的持久性”。 反对注入a的论点是它太重了,因此不是必需的,这就是我们需要的。而且,由于Spring将为每个Web请求创建一个DAO的新实例(我对此表示怀疑),因此不会有任何并发​

  • 我试图使用spring boot将EntityManager注释注入到我的DAO中,但是得到一个< code > InvalidDataAccessApiUsageException 消息,说没有可用的事务EntityManager。我的印象是,只要Spring Boot从< code>application.yml中获得了我的数据源信息,并且我用< code>@PersitenceContext

  • 我一直在研究很多类似的问题,这些问题并没有反映我的确切问题。如果我忽略了有人已经解决了这个问题,请让我知道。 我目前正在Wildfly 10.1上将一个旧的EJB CMP bean迁移到JPA。这里的问题是我的无状态会话beans中的entitymanager没有注入@PersistenceContext,使EntityManager为空。我尝试使用EntityManagerFactory来解决这