[nbj2ee] How can I refer a session bean in EJB module from WAR module in the same EE application?

  • From: "zszsamboki" < >
  • To:
  • Subject: [nbj2ee] How can I refer a session bean in EJB module from WAR module in the same EE application?
  • Date: Fri, 30 Nov 2012 06:22:52 -0500

Hello anybody!

I want to create an example java EE application (EE_App) in NetBeans 7.1 with 
JSF 2.0, EJB 3.1 and JPA. So I have made an EE application with an EJB and a 
web module.
EJB module (EE_App-ejb) contains an entity (beans.persistence.Message.java) 
and a stateless session bean (beans.session.MessageBean.java 
name="messageBean")
WAR module (EE_App-war) contains a facelet (facelet.MessageView.xhtml) and a 
managed bean (beans.managed.MessageViewBean.java name="messageViewBean")
The project compilable but it ignore the managed bean reference. Because it 
unable to inject the session bean instance into  the managed bean. 
Runtime error : "SEVERE: Class [ Lbeans/session/MessageBean; ] not found. 
Error while loading [ class beans.managed.MessageViewBean ]"

Message.java:
package beans.persistence;

import java.io.Serializable;
import javax.persistence.*;
import lombok.Getter;
import lombok.Setter;

@Entity
@NamedQueries( {
@NamedQuery( name="getAllMessages", query="SELECT m FROM message m" ),
@NamedQuery( name="getMessageByID", query="SELECT m FROM message m WHERE 
id=?1" ),
@NamedQuery( name="getMessageCount", query="SELECT COUNT( m ) FROM message m" 
)
})
public class Message implements Serializable
{
  private static final long serialVersionUID = 1L;
  @Id
  @Getter
  @Setter
  @GeneratedValue( strategy = GenerationType.AUTO )
  private Long id;

  @Getter
  @Setter
  private String message;
  
  @Override
  public int hashCode()
  {
    int hash = 0;
    hash += ( id != null ? id.hashCode() : 0 );
    return hash;
  }

  @Override
  public boolean equals( Object object )
  {
    // TODO: Warning - this method won't work in the case the id fields are 
not set
    if ( !( object instanceof Message ) )
    {
      return false;
    }
    Message other = (Message) object;
    if ( ( this.id == null && other.id != null ) || ( this.id != null && 
!this.id.equals( other.id ) ) )
    {
      return false;
    }
    return true;
  }

  @Override
  public String toString()
  {
    return "Message{" + "id=" + id + ", message=" + message + '}';
  }

  
}

MessageView.java:
package beans.session;

import beans.persistence.Message;
import java.util.ArrayList;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless( name="messageBean" )
@LocalBean
public class MessageBean
{
  @PersistenceContext( unitName = "EnterpriseApplication1-ejbPU" )
  private EntityManager em;

  public void persist( Object object )
  {
    em.persist( object );
  }

  // Add business logic below. (Right-click in editor and choose
  // "Insert Code > Add Business Method")

  public ArrayList<Message> getAllMessages()
  {
    Query q = em.createNamedQuery( "getAllMessages" );
    return (ArrayList<Message>) q.getResultList();
  }

  public Message getMessage( Long id_ )
  {
    Query q = em.createNamedQuery( "getMessageByID" ).setParameter( 1, id_ );
    return (Message) q.getSingleResult();
  }

  public String getMessageCount()
  {
    Query q = em.createNamedQuery( "getMessageCount" );
    return ((Long) q.getSingleResult()).toString();
  }
  
}

MessageViewBean.java:
package beans.managed;

import beans.persistence.Message;
import beans.session.MessageBean;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean( name="messageViewBean" )
@RequestScoped
public class MessageViewBean
{
  @EJB
  private MessageBean messageBean;

  /**
   * Creates a new instance of MessageViewBean
   */
  public MessageViewBean()
  {
  }

  public String getMessageCount()
  {
    return messageBean.getMessageCount();
  }

  public Message getMessage( Long id_ )
  {
    return messageBean.getMessage( id_ );
  }

  public ArrayList<Message> getAllMessages()
  {
    return messageBean.getAllMessages();
  }
}


I hear about the ejb-local-ref in the web.xml and the EJB annotation 
parameters. But I couldn't set the right values. I tried JNDI lookup in life 
cycle methods but the result was the same.


web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" ;
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ;
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ;
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd";>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
    <ejb-local-ref>
      <ejb-ref-name>ejb/messageBean</ejb-ref-name>
      <ejb-ref-type>Session</ejb-ref-type>
      <local>beans.session.MessageBean</local>
    </ejb-local-ref>
</web-app>


Compiler error:
SEVERE: Exception while deploying the app [EE_App-war] : Cannot resolve 
reference Local ejb-ref name=ejb/messageBean,Local 
3.x interface 
=beans.session.MessageBean,ejb-link=null,lookup=,mappedName=,jndi-name=,refType=Session

Could anybody help me to reference the ejb module from war module?






[nbj2ee] How can I refer a session bean in EJB module from WAR module in the same EE application?

zszsamboki 11/30/2012

[nbj2ee] Re: How can I refer a session bean in EJB module from WAR module in the same EE application?

John Yeary 11/30/2012

[nbj2ee] How can I refer a session bean in EJB module from WAR module in the same EE application?

zszsamboki 11/30/2012

[nbj2ee] Re: How can I refer a session bean in EJB module from WAR module in the same EE application?

John Yeary 11/30/2012

Project Features

About this Project

www was started in November 2009, is owned by jpirek, and has 21 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20140418.2d69abc). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close