2012-07-25 83 views
0

我有以下代碼:如何讓一個EntityManager被注入?

MonsterEJB.java:

package model; 

import java.util.List; 

import javax.ejb.LocalBean; 
import javax.ejb.Stateless; 
import javax.inject.Inject; 
import javax.inject.Named; 
import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.PersistenceContext; 
import javax.persistence.PersistenceUnit; 
import javax.persistence.Query; 

@Stateless 
@LocalBean 
@Named 
public class MonsterEJB 
{ 
    @PersistenceUnit(unitName="mongo") 
    private EntityManager em; 

    @Inject 
    private Monster injectableMonster; 

    public MonsterEJB() 
    { 

    } 

    public void create() 
    { 
     em.getTransaction().begin(); 
     Monster en = new Monster(); 
     en.setDescription(injectableMonster.getDescription()); 
     em.persist(en); 
     em.getTransaction().commit(); 
    } 

    public List<Monster> getList() 
    { 
     Query query = em.createQuery("Select m from Monster m"); 
     List<Monster> Monsters = query.getResultList(); 

     return Monsters; 
    } 

    public void remove() 
    { 
     em.getTransaction().begin(); 
     Monster en = em.find(Monster.class, injectableMonster.getId()); 
     em.remove(en); 
     em.getTransaction().commit(); 
    } 

    public void update() 
    { 
     em.getTransaction().begin(); 
     Monster en = em.find(Monster.class, injectableMonster.getId()); 
     en.setDescription(injectableMonster.getDescription()); 
     em.getTransaction().commit(); 
    } 
} 

Monster.java:

package model; 

import java.io.Serializable; 

import javax.enterprise.context.RequestScoped; 
import javax.inject.Named; 
import javax.persistence.*; 

import org.eclipse.persistence.nosql.annotations.*; 

@Entity 
@Named 
@RequestScoped 
@NoSql(dataFormat = DataFormatType.MAPPED) 
public class Monster implements Serializable 
{ 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue 
    @Field(name = "_id") 
    private String id; 

    @Version 
    private long version; 

    @Basic 
    private String description; 

    public Monster() 
    { 

    } 

    public String getId() 
    { 
     return id; 
    } 

    public void setId(String id) 
    { 
     this.id = id; 
    } 

    public String getDescription() 
    { 
     return description; 
    } 

    public void setDescription(String description) 
    { 
     this.description = description; 
    } 

    @Override 
    public String toString() 
    { 
     return "Monster [id=" + id + ", version=" + version + ", description="+ description; 
    } 
} 

的persistence.xml:

<?xml version="1.0" encoding="UTF-8" ?> 
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 
     version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"> 
    <persistence-unit name="mongo" transaction-type="RESOURCE_LOCAL"> 
     <class>model.Monster</class> 
     <properties> 
      <property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/> 
      <property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/> 
      <property name="eclipselink.nosql.property.mongo.port" value="27017"/> 
      <property name="eclipselink.nosql.property.mongo.host" value="localhost"/> 
      <property name="eclipselink.nosql.property.mongo.db" value="MonsterDatabase"/> 
      <property name="eclipselink.logging.level" value="FINEST"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

Show.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core"> 

<ui:composition template="WEB-INF/templates/BasicTemplate.xhtml"> 

    <ui:define name="header"> 
     <div style="width:100%;font-size:36px;line-height:48px;background-color:navy;color:white">Showing Monsters</div> 
    </ui:define> 

    <ui:define name="content"> 
     <h:dataTable var="f2" value="#{MonsterEJB.list}"> 
      <h:column>#{f2.id}, #{f2.description}</h:column> 
     </h:dataTable> 
    </ui:define> 

</ui:composition> 
</html> 

錯誤該網頁顯示

type Exception report 

message 

descriptionThe server encountered an internal error() that prevented it from fulfilling this request. 

exception 

javax.servlet.ServletException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB 
root cause 

javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB 
root cause 

javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB 
root cause 

javax.ejb.CreateException: Could not create stateless EJB 
root cause 

java.lang.IllegalStateException: Exception attempting to inject Env-Prop: model.MonsterEJB/[email protected] Resource. Class name = model.MonsterEJB Field [email protected]@@@ into class model.MonsterEJB 
root cause 

com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Env-Prop: model.MonsterEJB/[email protected] Resource. Class name = model.ElnotEJB Field [email protected]@@@ into class model.MonsterEJB 
root cause 

java.lang.IllegalArgumentException: Can not set javax.persistence.EntityManager field model.MonsterEJB.em to com.sun.enterprise.container.common.impl.EntityManagerFactoryWrapper 

問: 如何讓我的MonsterEJB.java注入與NoSQL的一個EntityManager的?

回答

1

我不確定這是否是您例外的原因,但您的MonsterEJB類同時註釋爲@Named@Stateless@Named用於定義JSF Managed Bean@Stateless對於Enterprise Java Beans,這是不同的事情。

使用一個或另一個註釋。

此外,Monster類是在定義爲同一時間:一個Entity,作爲ManagedBean通過@Named註釋,並通過javax.enterprise.context.RequestScoped註釋的CDI豆。而且這也沒有意義。

爲了澄清這些概念,我建議學習一些Java EE 6教程(如this)。

+0

應該如何Monster.java和MonsterEJB正確地然後註釋?我在我的Java EE 6應用程序中使用java服務器面。給我的印象是我需要這樣才能正常工作。 – stackoverflow 2012-07-25 15:41:48

+0

在你學習了一個教程之後,你可以實現這個來自Adam Bien博客的[基本JPA應用](http://www.adam-bien.com/roller/abien/entry/ejb_3_persistence_jpa_for),如果你想要更多的東西完成,那麼[this](http://wiki.netbeans.org/DevelopJavaEE6App)是一個使用JPA和其他Java EE 6功能的完整Web應用程序... – perissf 2012-07-25 17:08:16

2

嘗試使用@PersistenceContext,而不是@PersistenceUnit

+0

謝謝!作品 – 2016-10-24 14:02:57