【问题标题】:Inject PersistenceContext from persistence.xml从 persistence.xml 注入 PersistenceContext
【发布时间】:2018-05-22 20:52:09
【问题描述】:

我正在尝试使用 PersistenceContext 注释来注入实体管理器,但出现以下异常。

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'Foo' available

然后我做了一些研究,在每个示例中,配置类中都有一个 bean,其信息与我在 persistence.xml 中的信息相同。

我们是否应该能够仅使用持久性单元名称注入实体管理器?

这是我的代码

@Component
public class UnitOfWork {
    @PersistenceContext(unitName="Foo")
    private EntityManager entityManager;
}

@Configuration
@ComponentScan("com.foo.package")
public class Config {
}

META-INF 文件夹中的 Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
  version="2.1">


    <persistence-unit name="Foo">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect"/>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/foo?useSSL=false" />
            <property name="hibernate.connection.username" value="foo"/>
            <property name="hibernate.connection.password" value="foo" />
        </properties>
    </persistence-unit>
</persistence>

Pom.xml

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org-springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org-springframework.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.0.7.RELEASE</version>
    </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>5.0.6.RELEASE</version>
    </dependency>

【问题讨论】:

  • UnitOfWork 类是否位于 com.foo.package 包内?
  • @Nikolas 是的。它正在扫描注释,但找不到名为 Foo 的 bean,因为我没有。我不知道当你有一个 persistence.xml 时是否需要它
  • 你的配置中仍然需要一个LocalContainerEntityManagerFactory bean,因为这使得EntityManagerFactory 在应用程序上下文中可用。如果没有一个不起作用,LocalContainerEntityManagerFactory 将使用persistence.xml(如果存在)来配置自己。
  • @M.Deinum 它自己找到persistence.xml还是我必须使用jndi?
  • 为什么需要 JNDI?如果它在默认位置,它将被自动拾取。

标签: java spring hibernate jpa


【解决方案1】:

您仍然需要定义LocalContainerEntityManagerFactory 以使EntityManagerFactory 在应用程序上下文中可用。 如果没有一个不起作用,LocalContainerEntityManagerFactory 将使用persistence.xml(如果存在)来配置自己。

【讨论】:

    猜你喜欢
    • 2011-06-10
    • 2017-05-19
    • 2015-02-14
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 2012-11-21
    • 1970-01-01
    相关资源
    最近更新 更多