quarkus:带注入的休眠实体管理器

quarkus: hibernate entityManger with inject

根据Quarkus Documentation,我们可以通过@Inject注入一个线程安全的entityManger。

@ApplicationScoped
public class SantaClausService {
    @Inject
    EntityManager em; 

    @Transactional 
    public void createGift(String giftDescription) {
        Gift gift = new Gift();
        gift.setName(giftDescription);
        em.persist(gift);
    }
}

但是,当我这样做时,警告:

No bean is eligible for injection to the injection point

来了。

尝试编译时,出现此错误:

Caused by: javax.enterprise.inject.UnsatisfiedResolutionException:
Unsatisfied dependency for type javax.persistence.EntityManager 
and qualifiers [@Default]

这意味着,我必须通过我必须创建并分配给该 entityManager 的注释来限定注入的 entityManger。这在 @PersistenceContext EntityManager em 的 jee 中是不需要的。

Quarkus 中是否有任何准备好的 entityManager 已经合格并且可以无警告地使用?

你的 Gift@Entity 注释吗?

这个错误有点误导。 如果你没有 @Entity class 你也会得到这个错误。

我试过了...没有 @Entity 得到错误,然后我将 @Entity 添加到 class 一切正常。