Hjælp til J2EE, EJB3 og jboss-5.0.0.GA
Hej Eksperter,jeg er lige gået i gang med at lære J2EE, men er desværre løbet ind i problemer med et meget simpelt eksempel:
com.titan.domai:
Cabin.java
package com.titan.domain;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Id;
@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;
}
}
-------------------------------
com.titan.travelagent:
TravelAgentRemote.java
TravelAgentBean.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 pKey);
}
--------------------------------------------------
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);
}
}
META-INF:
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="titan">
<jta-data-source>java:/DefaultDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
Dette bliver kompileret til titan.jar som kopiers til:
/jboss-5.0.0.GA/server/default/deploy
Klientkode:
import com.titan.travelagent.TravelAgentRemote;
import com.titan.domain.Cabin;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
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)ref;
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();
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);
}
}
Jeg benytter ant til at kompiler og eksekver koden:
<?xml version="1.0"?>
<project name="test" default="run" basedir=".">
<property name="src" value="."/>
<property name="version" value="1.0"/>
<property name="build" value="build"/>
<property name="classes" value="${build}/class"/>
<property name="jboss" value="C:/jboss-5.0.0.GA"/>
<path id="classpath">
<fileset dir="${jboss}/server/default/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${jboss}/lib">
<include name="**/*jar*"/>
</fileset>
<fileset dir="${jboss}/client">
<include name="*.jar"/>
</fileset>
<fileset dir="../${build}">
<include name="titan.jar" />
</fileset>
<pathelement location="${src}/client-config"/>
</path>
<target name="compile">
<javac srcdir="${src}"
destdir="${src}"
debug="on"
deprecation="on"
optimize="off"
includes="**">
<classpath refid="classpath"/>
</javac>
</target>
<target name="jar" depends="compile">
<jar destfile="Client.jar" basedir="." includes="**/*.class" />
</target>
<target name="run" depends="jar">
<java classname="Client" classpath="Client.jar" fork="true"/>
<classpath refid="classpath"/>
</target>
</project>
Når jeg eksekver programmet får jeg følgende fejl:
javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
Jeg kan se at org.jnp.interfaces.NamingContextFactory mangler på runtime, men hvordan får jeg den inkluderet ?!