JSF Deploy Exception:类型EntityManager的不满意依赖项

Goo*_*oot 5 jsf jboss inject java-ee maven

我只是按照门票怪物教程(http://www.jboss.org/jdf/examples/ticket-monster/tutorial/Introduction/)并在我的解决方案中添加了一个休息服务类.

package projectFoo.rest;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import projectFoo.model.party;


@Path("/partys")
@RequestScoped
public class partyService {

@Inject
private EntityManager em;

@GET
@Produces(MediaType.APPLICATION_JSON)
 public List<party> getAllEvents() {
    @SuppressWarnings("unchecked")
    final List<party> results =
            em.createQuery(
            "select e from party e order by e.name").getResultList();
    return results;
}
}
Run Code Online (Sandbox Code Playgroud)

@Inject加下划线,返回:"没有bean有资格注入注入点[JSR-299§5.2.1]"

当我尝试部署包时,该过程将失败并返回以下消息:

Unsatisfied dependencies for type [EntityManager] with qualifiers [@Default] at injection point.
Run Code Online (Sandbox Code Playgroud)

我是否必须为实体管理器添加一个bean?这个怎么样?该教程没有提到这一点.实际上我在最终的ticket-monster项目中找不到任何bean的定义.

kos*_*tja 8

EntityManager在未启用该CDI神器发现(JPA提供商罐子没有beans.xml包括在内).

您可以使用"good old" @PersistenceContext注释insead @Inject,或者如果要使用@Inject,则需要为EntityManager提供生成器,如下所示:

class Resources {
   @SuppressWarnings("unused")
   @Produces
   @PersistenceContext
   private EntityManager em;
...
Run Code Online (Sandbox Code Playgroud)