9 Replies Latest reply on Sep 13, 2013 7:18 AM by Arun Varadharajan

    java.lang.ClassCastException: javax.naming.Reference cannot

    Jae-Woo Lee Newbie

      Hi.

      I want to test a simple Entity Persistence program.
      But I got the following exception.
      Any idea what I did wrong ?
      I am using JBoss 5.0.0.GA.
      The example is from a EJB3 tutorial.
      Thank you in advance.

      Sincerely,
      Jaewoo

      ===================
      Exception in thread "main" java.lang.ClassCastException
      at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
      at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
      at com.titan.clients.Client.main(Client.java:16)
      Caused by: java.lang.ClassCastException: javax.naming.Reference cannot be cast to org.omg.CORBA.Object
      at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212)
      ... 2 more
      Java Result: 1
      =========================
      == Client.java ===========
      package com.titan.clients;

      import com.titan.travelagent.TravelAgentRemote;
      import com.titan.domain.Cabin;

      import javax.naming.Context;
      import java.util.Properties;
      import javax.rmi.PortableRemoteObject;

      public class Client {

      public static void main(String[] args) {
      try {
      Context jndiContext = getInitialContext();
      Object ref = jndiContext.lookup("TravelAgentBean/remote");
      TravelAgentRemote dao = (TravelAgentRemote) PortableRemoteObject.narrow(ref, TravelAgentRemote.class);

      Cabin cabin_1 = new Cabin();
      cabin_1.setId(1);
      cabin_1.setName("Master Suite");
      cabin_1.setDeckLevel(1);
      cabin_1.setShipId(1);
      cabin_1.setBedCount(3);

      dao.createCabin(cabin_1);

      Cabin cabin_2 = dao.findCabin(1);
      System.out.println(cabin_2.getName());
      System.out.println(cabin_2.getDeckLevel());
      System.out.println(cabin_2.getShipId());
      System.out.println(cabin_2.getBedCount());

      } catch (javax.naming.NamingException ne) {
      ne.printStackTrace();
      }
      }

      public static Context getInitialContext()
      throws javax.naming.NamingException {

      Properties p = new Properties();
      // ... Specify the JNDI properties specific to the vendor.
      p.put(Context.INITIAL_CONTEXT_FACTORY,
      "org.jnp.interfaces.NamingContextFactory");
      p.put(Context.URL_PKG_PREFIXES,
      "org.jboss.naming:org.jnp.interfaces");
      p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
      return new javax.naming.InitialContext(p);
      }
      }

      == TravelAgentRemove.java ====

      package com.titan.travelagent;

      import javax.ejb.Remote;
      import com.titan.domain.Cabin;

      @Remote
      public interface TravelAgentRemote {

      public void createCabin(Cabin cabin);
      public Cabin findCabin(int id);
      }

      == TravelAgentBean.java =========
      package com.titan.travelagent;

      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;

      import javax.persistence.PersistenceContext;

      import com.titan.domain.Cabin;

      @Stateless
      public class TravelAgentBean implements TravelAgentRemote {

      @PersistenceContext(unitName = "titan")
      private EntityManager manager;

      public void createCabin(Cabin cabin) {
      manager.persist(cabin);
      }

      public Cabin findCabin(int pKey) {
      return manager.find(Cabin.class, pKey);
      }
      }

      == Cabin.java =========
      package com.titan.domain;

      import javax.persistence.*;

      @Entity
      @Table(name="CABIN")
      public class Cabin implements java.io.Serializable{
      private int id;
      private String name;
      private int deckLevel;
      private int shipId;
      private int bedCount;

      @Id
      @Column(name="ID")
      public int getId( ) { return id; }
      public void setId(int pk) { id = pk; }

      @Column(name="NAME")
      public String getName( ) { return name; }
      public void setName(String str) {name = str; }

      @Column(name="DECK_LEVEL")
      public int getDeckLevel( ) { return deckLevel; }
      public void setDeckLevel(int level) { deckLevel = level; }

      @Column(name="SHIP_ID")
      public int getShipId( ) { return shipId; }
      public void setShipId(int sid) { shipId = sid; }

      @Column(name="BED_COUNT")
      public int getBedCount( ) { return bedCount; }
      public void setBedCount(int bed) { bedCount = bed; }

      }

      == persistence.xml =======
      <?xml version="1.0" encoding="UTF-8"?>

      <persistence-unit name="titan" transaction-type="JTA">
      org.hibernate.ejb.HibernatePersistence
      <jta-data-source>java:/DefaultDS</jta-data-source>



      </persistence-unit>