Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject PersistenceContext from persistence.xml

I'm trying to use the PersistenceContext annotation to inject an entity manager but I get the following exception.

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

Then I did some research and in every example there's a bean in the configuration class with the same information that I have in my persistence.xml.

Are we supposed to be able to inject the entitymanager with only the persistence-unit name?

Here's my code

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

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

Persistence.xml in META-INF folder

<?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>
like image 541
Marc Avatar asked Mar 28 '26 13:03

Marc


1 Answers

You still need to define a LocalContainerEntityManagerFactory to have the EntityManagerFactory available in the application context. Without one that wouldn't work, the LocalContainerEntityManagerFactory will use the persistence.xml (when present) to configure itself.

like image 57
M. Deinum Avatar answered Mar 30 '26 04:03

M. Deinum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!