Home > other >  Spring Boot Data Jpa - entityManager is null
Spring Boot Data Jpa - entityManager is null

Time:02-05

I switched from jakarta to javax and started getting this error; (i have to use javax); I don't understand why it worked before and now it cant inject EntityManager it my first question here, sorry for mistakes

application.yaml:

spring:
  jpa:
    hibernate:
      ddl-auto: update
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/stage3_module3
    username: postgres
    password: root
@SpringBootApplication
public class NewsManagementApplication {
    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(NewsManagementApplication.class, args);
        NewsManagementMenu menu = context.getBean(NewsManagementMenu.class);
        menu.setScanner(new Scanner(System.in));
        menu.runMenu();
    }
}
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Optional;


@Repository
public class NewsRepository implements BaseRepository<NewsModel, Long> {
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    @SuppressWarnings("unchecked")
    public List<NewsModel> readAll() {
        return entityManager.createQuery("select n from NewsModel n").getResultList();
    }

    @Override
    public Optional<NewsModel> readById(Long id) {
        return Optional.ofNullable(entityManager.find(NewsModel.class, id));
    }

    @Transactional
    @Override
    public NewsModel create(NewsModel entity) {
        entityManager.persist(entity);
        return entity;
    }

    @Transactional
    @Override
    public NewsModel update(NewsModel entity) {
        Optional<NewsModel> maybeNull = readById(entity.getId());
        if (maybeNull.isEmpty()) {
            return null;
        }
        NewsModel toUpdate = maybeNull.get();
        toUpdate.setTitle(entity.getTitle());
        toUpdate.setContent(entity.getContent());
        toUpdate.setLastUpdateDate(entity.getLastUpdateDate());
        toUpdate.setAuthor(entity.getAuthor());
        toUpdate.setTags(entity.getTags());
        return toUpdate;
    }

    @Transactional
    @Override
    public boolean deleteById(Long id) {
        return entityManager.createQuery("delete from NewsModel n where n.id=:id")
                .setParameter("id", id)
                .executeUpdate() != 0;
    }

    @Override
    public boolean existById(Long id) {
        return readById(id).isPresent();
    }
}
dependencies {
        implementation "org.springframework:spring-context:$springVersion"
        testImplementation "org.springframework:spring-test:$springVersion"
        testImplementation "org.junit.jupiter:junit-jupiter-api:$jupiterVersion"
        testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$jupiterVersion"
        testImplementation "com.tngtech.archunit:archunit-junit5:$archUnitVersion"
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.0.2'

        implementation 'org.postgresql:postgresql:42.5.2'
        testImplementation 'org.mockito:mockito-core:5.1.1'
        testImplementation 'org.mockito:mockito-junit-jupiter:5.1.1'
        implementation 'javax.persistence:persistence-api:1.0'
    }

it gives me: Cannot invoke "javax.persistence.EntityManager.createQuery(String)" because "this.entityManager" is null

update: problem solved I changed version of spring-boot-starter-data-jpa to 2.7.7 and add this dependency: 'javax.xml.bind:jaxb-api:2.3.1'

CodePudding user response:

The only thing I can think of is that @PersistenceContextannotation is not properly configured -> means that the entityManager instance variable is not being properly initialized or injected.

Try to see if the answer from this post helps: java.lang.NullPointerException: Cannot invoke "javax.persistence.EntityManager error in Spring MVC while pulling data

CodePudding user response:

problem was solved I changed version of spring-boot-starter-data-jpa to 2.7.7 and add this dependency: 'javax.xml.bind:jaxb-api:2.3.1'

  • Related