Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support @Inject EntityManager #2207

Open
hantsy opened this issue Jul 11, 2024 · 0 comments
Open

Support @Inject EntityManager #2207

hantsy opened this issue Jul 11, 2024 · 0 comments

Comments

@hantsy
Copy link

hantsy commented Jul 11, 2024

JPA 3.2 should support injecting an EntityManager or EntityManagerFactory by @Inject.

I created a simple example to verify it but found it did not work as expected.

The testing env is:

  • Java 21
  • Glassfish 8.0.0 M6 with built-in EclipseLink 5.0.0 B02

Add scope and qualifier in the persistence.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="3.2" xmlns="https://jakarta.ee/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_2.xsd">
    <persistence-unit name="defaultPU" transaction-type="JTA">
        <qualifier>com.example.employee.MyCustom</qualifier>
        <scope>jakarta.enterprise.context.ApplicationScoped</scope>
....

I tried to inject it with the custom qualifier like this.

@ApplicationScoped
public class EmployeeRepository {

    @Inject
    @MyCustom
    EntityManager entityManager;

    public List<Employee> allEmployees() {
        return entityManager.createQuery("select p from Employee p", Employee.class)
                .getResultList();
    }
}

And I got the following exception:

Unsatisfied dependencies for type EntityManager with qualifiers @MyCustom
  at injection point [BackedAnnotatedField] @Inject @MyCustom com.example.employee.EmployeeRepository.entityManager
  at com.example.employee.EmployeeRepository.entityManager(EmployeeRepository.java:0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant