发生错误:在JNDI中找不到EntityManagerFactory:java:/ seam1EntityManagerFactory

时间:2012-05-24 17:33:44

标签: java jsf jboss seam

我是jboss的新手,当我尝试列出数据库的联系人时,我发现了这个错误,我正在使用以下软件:
jboss-4.2.3.GA
jboss-seam-2.2.2.Final
PostgreSQL 8.4
Eclipse Helios + JBoss工具
这是Stack Strace(只有第一个错误):

Caused by: javax.naming.NameNotFoundException: seam1EntityManagerFactory not bound
    at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
    at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
    at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
    at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:667)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBinding(ManagedPersistenceContext.java:257)
    ... 117 more

contato_list.xhtml:

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

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:s="http://jboss.com/products/seam/taglib"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:rich="http://richfaces.org/rich" template="layout/template.xhtml">

    <ui:define name="body">

        <h:messages globalOnly="true" styleClass="message" id="globalMessages" />

        <rich:panel>
            <f:facet name="header">Contatos</f:facet>
            <div class="results" id="contatoList">
                <rich:dataTable id="contatoList" var="_contato" value="#{contatos}">
                    <h:column>
                        <f:facet name="header">Id</f:facet>
                        #{_contato.id}
                    </h:column>
                    <h:column>
                        <f:facet name="header">Nome</f:facet>
                        #{_contato.nome}
                    </h:column>
                    <h:column>
                        <f:facet name="header">Email</f:facet>
                        #{_contato.email}
                    </h:column>
                    <h:column>
                        <f:facet name="header">Ação</f:facet>
                        <s:link id="editar" action="#{contatoAction.editarContato}"
                            value="Editar" />
                        <s:link id="remover" action="#{contatoAction.removerContato}"
                            value="Remover" />
                    </h:column>
                </rich:dataTable>
            </div>
        </rich:panel>
        <s:div styleClass="actionButtons">
            <s:button id="novo" action="#{contatoAction.novoContato}" value="Novo contato"/>
        </s:div>
    </ui:define>
</ui:composition>

ContatoAction.java:

package org.domain.seam1.session;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import org.domain.seam1.entity.Contato;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;

@Name("contatoAction")
@Scope(ScopeType.CONVERSATION)
public class ContatoAction implements Serializable{

    private static final long serialVersionUID = 4121233854017478226L;

    @In
    private EntityManager entityManager;

    @SuppressWarnings("unused")
    @DataModel
    private List<Contato> contatos;

    @DataModelSelection
    @Out(required=false)
    private Contato contato;

    @Factory("contatos")
    @SuppressWarnings("unchecked")
    public void listarContatos() {
        //seleciona todos os contatos
        contatos = entityManager.createQuery("select c from Contato c").getResultList();
    }

    @Begin
    public String novoContato() {
        contato = new Contato();

        return "novoContato";
    }

    @Begin
    public String editarContato() {
        //atualiza os dadis do contato para fazer a edição
        entityManager.refresh(contato);

        return "editarContato";
    }

    public String removerContato() {
        //remove o contato
        entityManager.remove(contato);

        return "contatoRemovido";
    }

    @End
    public String salvarContato() {
        //se não tem id é porque deve ser inserido, caso contrário alterado
        if (contato.getId() == null) {
            entityManager.persist(contato);
        } else {
            entityManager.merge(contato);
        }

        return "contatoSalvo";
    }
}

Contato.java:

package org.domain.seam1.entity;


import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


@Entity
public class Contato implements Serializable {

    private static final long serialVersionUID = 4121233854017478226L;

    @Id @GeneratedValue
    private Long id;

    @Column(length=50, nullable=false)
    private String nome;

    @Column(length=50, nullable=false)
    private String email;

    public Long getId() {
        return id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((email == null) ? 0 : email.hashCode());
        result = prime * result + ((nome == null) ? 0 : nome.hashCode());
        return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Contato))
            return false;
        Contato other = (Contato) obj;
        if (email == null) {
            if (other.email != null)
                return false;
        } else if (!email.equals(other.email))
            return false;
        if (nome == null) {
            if (other.nome != null)
                return false;
        } else if (!nome.equals(other.nome))
            return false;
        return true;
    }

}

1 个答案:

答案 0 :(得分:1)

只需将antlr.jar文件从antlr-2.7.6.jar更改为antlr-3.1.1.jar即可解决我的错误