[jboss-user] [Beginners Corner] - Re: Cannot Get InitialContext Lookup to Work
kurzweil4
do-not-reply at jboss.com
Mon Aug 6 12:35:39 EDT 2007
My code has various things commented out that I have experimented with.
Here is the persistence.xml. I am sure that my data connection is working properly because I tested it by manually opening the data connection in a JSP.
| <?xml version="1.0" encoding="windows-1252" ?>
| <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_1_0.xsd"
| version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
| <persistence-unit name="Model">
| <jta-data-source>java:/hr</jta-data-source>
| <class>ice.data.table.model.Departments</class>
| <!--class>ice.data.table.model.DepPublicFacade</class>
| <class>ice.data.table.model.DepPublicFacadeLocal</class>
| <class>ice.data.table.model.DepPublicFacadeBean</class-->
| <properties>
| <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
| <property name="hibernate.hbm2ddl.auto" value="validate"/>
| </properties>
| </persistence-unit>
| </persistence>
|
Here in the web.xml, I tried to create an ejb reference, but the problem I ran into was that JBoss gave me an error that it was expecting a local home tag, and since I am using EJB 3.0, I don't have one. I updated the document schema version to 2.5, but JBoss still asked for a local home tag.
| <?xml version = '1.0' encoding = 'windows-1252'?>
| <!--web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"-->
| <web-app version= "2.5"
| 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_2_5.xsd">
| <description>Empty web.xml file for Web Application</description>
| <context-param>
| <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
| <param-value>server</param-value>
| </context-param>
| <context-param>
| <param-name>javax.faces.application.CONFIG_FILES</param-name>
| <param-value>/WEB-INF/faces-config.xml</param-value>
| </context-param>
| <!--ejb-local-ref>
| <ejb-ref-name>ejb/DepPublicFacade</ejb-ref-name>
| <ejb-ref-type>Session</ejb-ref-type>
| <local>ice.data.table.model.DepPublicFacade</local>
| <ejb-link>DepPublicFacade</ejb-link>
| </ejb-local-ref-->
| <listener>
| <listener-class>com.icesoft.faces.util.event.servlet.ContextEventRepeater</listener-class>
| </listener>
| <listener>
| <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
| </listener>
| <servlet>
| <servlet-name>Faces Servlet</servlet-name>
| <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
| <load-on-startup>1</load-on-startup>
| </servlet>
| <servlet>
| <servlet-name>Persistent Faces Servlet</servlet-name>
| <servlet-class>com.icesoft.faces.webapp.xmlhttp.PersistentFacesServlet</servlet-class>
| <load-on-startup>1</load-on-startup>
| </servlet>
| <servlet>
| <servlet-name>Blocking Servlet</servlet-name>
| <servlet-class>com.icesoft.faces.webapp.xmlhttp.BlockingServlet</servlet-class>
| <load-on-startup>1</load-on-startup>
| </servlet>
| <servlet>
| <servlet-name>uploadServlet</servlet-name>
| <servlet-class>com.icesoft.faces.component.inputfile.FileUploadServlet</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>
| <servlet-mapping>
| <servlet-name>Persistent Faces Servlet</servlet-name>
| <url-pattern>/xmlhttp/*</url-pattern>
| </servlet-mapping>
| <servlet-mapping>
| <servlet-name>Persistent Faces Servlet</servlet-name>
| <url-pattern>*.iface</url-pattern>
| </servlet-mapping>
| <servlet-mapping>
| <servlet-name>Persistent Faces Servlet</servlet-name>
| <url-pattern>*.jspx</url-pattern>
| </servlet-mapping>
| <servlet-mapping>
| <servlet-name>Blocking Servlet</servlet-name>
| <url-pattern>/block/*</url-pattern>
| </servlet-mapping>
| <servlet-mapping>
| <servlet-name>uploadServlet</servlet-name>
| <url-pattern>/uploadHtml</url-pattern>
| </servlet-mapping>
| <session-config>
| <session-timeout>35</session-timeout>
| </session-config>
| <mime-mapping>
| <extension>html</extension>
| <mime-type>text/html</mime-type>
| </mime-mapping>
| <mime-mapping>
| <extension>txt</extension>
| <mime-type>text/plain</mime-type>
| </mime-mapping>
| </web-app>
|
My Entity. I tried various ways of structuring the JNDI name for lookup() from various web sites, but none of them worked.
| package ice.data.table.model;
|
| import java.io.Serializable;
|
| import javax.annotation.Resource;
|
| import javax.persistence.Column;
| import javax.persistence.Entity;
| import javax.persistence.Id;
| import javax.persistence.NamedQuery;
|
| @Entity
| @NamedQuery( name = "Departments.findAll",
| query = "select o from Departments o" )
| public class Departments implements Serializable
| {
| @Id
| @Column( name="DEPARTMENT_ID", nullable = false )
| private Long departmentId;
| @Column( name="DEPARTMENT_NAME", nullable = false )
| private String departmentName;
| @Column( name="LOCATION_ID" )
| private Long locationId;
| @Column( name="MANAGER_ID" )
| private Long managerId;
|
| public Departments()
| {
| }
|
| public Long getDepartmentId()
| {
| return departmentId;
| }
|
| public void setDepartmentId( Long departmentId )
| {
| this.departmentId = departmentId;
| }
|
| public String getDepartmentName()
| {
| return departmentName;
| }
|
| public void setDepartmentName( String departmentName )
| {
| this.departmentName = departmentName;
| }
|
| public Long getLocationId()
| {
| return locationId;
| }
|
| public void setLocationId( Long locationId )
| {
| this.locationId = locationId;
| }
|
| public Long getManagerId()
| {
| return managerId;
| }
|
| public void setManagerId( Long managerId )
| {
| this.managerId = managerId;
| }
| }
|
My service:
| package ice.data.table.model;
|
| import java.util.List;
|
| import javax.ejb.EJB;
| import javax.ejb.Stateless;
|
| import javax.persistence.EntityManager;
| import javax.persistence.PersistenceContext;
|
| @Stateless( name="DepPublicFacade" )
| public class DepPublicFacadeBean implements DepPublicFacade, DepPublicFacadeLocal
| {
| @PersistenceContext( unitName="Model" )
| private EntityManager em;
|
| public DepPublicFacadeBean()
| {
| }
|
| public Object mergeEntity( Object entity )
| {
| return em.merge(entity);
| }
|
| public Object persistEntity( Object entity )
| {
| em.persist(entity);
| return entity;
| }
|
| /** <code>select o from Departments o</code> */
| public List<Departments> queryDepartmentsFindAll()
| {
| return em.createNamedQuery("Departments.findAll").getResultList();
| }
|
| public void removeDepartments( Departments departments )
| {
| departments = em.find(Departments.class, departments.getDepartmentId());
| em.remove(departments);
| }
| }
|
My local and remotes:
| package ice.data.table.model;
|
| import java.util.List;
|
| import javax.ejb.Remote;
|
| @Remote
| public interface DepPublicFacade
| {
| Object mergeEntity( Object entity );
|
| Object persistEntity( Object entity );
|
| List<Departments> queryDepartmentsFindAll();
|
| void removeDepartments( Departments departments );
| }
|
| package ice.data.table.model;
|
| import java.util.List;
|
| import javax.ejb.Local;
|
| @Local
| public interface DepPublicFacadeLocal
| {
| Object mergeEntity( Object entity );
|
| Object persistEntity( Object entity );
|
| List<Departments> queryDepartmentsFindAll();
|
| void removeDepartments( Departments departments );
| }
|
And finally the bean from my WAR where I am calling the above code from the EJB JAR:
| package ice.data.table.view;
|
| import ice.data.table.model.DepPublicFacade;
| import ice.data.table.model.DepPublicFacadeLocal;
| import ice.data.table.model.Departments;
| import java.util.List;
|
| import javax.ejb.EJB;
|
| import javax.ejb.EJBContext;
|
| import javax.ejb.SessionContext;
|
| import javax.naming.Binding;
| import javax.naming.Context;
| import javax.naming.InitialContext;
| import javax.naming.NameClassPair;
| import javax.naming.NamingEnumeration;
| import javax.naming.NamingException;
|
| public class DepartmentsBean
| {
| private DepPublicFacade model;
|
| public DepartmentsBean()
| {
| try
| {
| final Context context = getInitialContext();
|
| /*
| NamingEnumeration<NameClassPair> ne = context.list( "java:comp/env" ); //"IceDataTable/DepPublicFacade.remote" );///DepPublicFacade/local" );
|
| NameClassPair ncp;
|
| System.out.println( ne.hasMore() );
|
| while ( ne.hasMore() )
| {
|
| ncp = ( NameClassPair ) ne.next();
|
| System.out.println( ncp.getName() );
| System.out.println( "\t" + ncp.getClass() );
| System.out.println( "\t" + ncp.getClassName() );
|
| }
| /*
| while ( ne.hasMore() )
| {
| ncp = ( NameClassPair ) ne.next();
| System.out.println( ncp.getName() + ": " + ncp.getClass() + ": " + ncp.getNameInNamespace() + ": " + ncp.getClassName() );
| }*/
| //model = (DepPublicFacade)context.lookup("DepPublicFacade");
| model = (DepPublicFacade)context.lookup("java:comp/env/DepPublicFacade");
| }
| catch (Exception ex)
| {
| ex.printStackTrace();
| }
| }
|
| public List getDepartments()
| {
| return model.queryDepartmentsFindAll();
| }
|
| private static Context getInitialContext() throws NamingException
| {
| // Get InitialContext for Embedded OC4J
| // The embedded server must be running for lookups to succeed.
| return new InitialContext();
| }
| }
|
Thank you for your response. Any help you can provide is appreciated.
Kurzweil4
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4071266#4071266
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4071266
More information about the jboss-user
mailing list