java Sping Boot 数据Jpa -实体管理器为空

rqcrx0a6  于 2023-02-07  发布在  Java
关注(0)|答案(2)|浏览(162)

我从jakarta切换到javax,开始得到这个错误;(我不得不使用javax);我不明白为什么它以前工作,现在它不能注入EntityManager它我的第一个问题在这里,对不起的错误
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'
    }

它给我:无法调用"javax. persistence.实体管理器. createQuery(字符串)",因为"此.实体管理器"为空
更新:问题解决我将spring-boot-starter-data-jpa的版本更改为2.7.7并添加此依赖项:"javax.xml绑定:jaxb应用程序接口:2.3.1"

o8x7eapl

o8x7eapl1#

我唯一能想到的是@PersistenceContext注解没有正确配置-〉意味着entityManager示例变量没有被正确初始化或注入。
试试看这篇文章的答案是否有帮助:java.lang.NullPointerException: Cannot invoke "javax.persistence.EntityManager error in Spring MVC while pulling data

tmb3ates

tmb3ates2#

我将spring-boot-starter-data-jpa的版本更改为2.7.7并添加此依赖项:“javax.xml绑定:jaxb应用程序接口:2.3.1”

相关问题