EJB3-Deploying Your First Beans

by admin ~ August 2, 2008

This is the example from the chapter “Developing your First Bean” from the book “Enterprise JavaBeans 3.0″ by Bill Bruke & Richard Monson-Haefel. This example I have tried it out on JBoss AS 5.0.0.CR1. I have used Postgresql as the database. And I have used eclipse WTP as IDE.

Entity Cabin: Cabin.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.titan.domain;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
 
@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; }
}

The persistence.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<persistence:persistence version="1.0" xmlns:persistence="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd ">
  <persistence:persistence-unit name="titan" transaction-type="JTA">
    <persistence:description>Chapter: 4</persistence:description>
    <persistence:jta-data-source>java:/TitanDB</persistence:jta-data-source>
    <persistence:properties>
    	<persistence:property name="hibernate.hbm2ddl.auto" value="create"/>
    </persistence:properties>
  </persistence:persistence-unit>
</persistence:persistence>

TravelAgentRemote: The Remote Interface

1
2
3
4
5
6
7
8
9
10
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: The Bean Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);
	}
}

The Data Source file: postgres-TitanDB-ds.xml

This file is deployed in the deploy directory of the JBoss Application Server. The JDBC driver “postgresql-8.1-412.jdbc3.jar” is also deployed in the lib directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
  <local-tx-datasource>
    <jndi-name>java:/TitanDB</jndi-name>
	<use-java-context>true</use-java-context>
    <connection-url>jdbc:postgresql://127.0.0.1:5432/sampledb</connection-url>
    <driver-class>org.postgresql.Driver</driver-class>
    <user-name>ranjan_dev</user-name>
    <password>ranjan_dev</password>
      <metadata>
         <type-mapping>PostgreSQL 8.0</type-mapping>
      </metadata>
  </local-tx-datasource>
</datasources>

Building the application: build.xml

The following build.xml file have used to build the application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
<project name="Packaging Generator" default="_packaging_generation_">
	<target name="_packaging_generation_" depends="TitanCruisesExample" />
	<target name="TitanCruisesExample" description="TitanCruises.ear" depends="TravelAgentBean">
			<jar destfile="TitanCruises.ear">
				<zipfileset dir="conf" prefix="META-INF">
					<include name="application.xml" />
				</zipfileset>
				<zipfileset dir="/home/ranjan/workspaceEJB3/TitanCruises">
					<include name="TravelAgentBean.jar" />
					<include name="Cabin.jar" />
				</zipfileset>
			</jar>
	</target>	
	<target name="TravelAgentBean" description="TravelAgentBean.jar" depends="Cabin">
		<jar destfile="TravelAgentBean.jar">
			<zipfileset dir="/home/ranjan/workspaceEJB3/TitanCruises/bin/com/titan/travelagent" prefix="com/titan/travelagent"/>
		</jar>
	</target>
	<target name="Cabin" description="Cabin.jar">
		<jar destfile="Cabin.jar">
			<zipfileset dir="conf" prefix="META-INF">
				<include name="persistence.xml" />
			</zipfileset>
			<zipfileset dir="/home/ranjan/workspaceEJB3/TitanCruises/bin/com/titan/domain" prefix="com/titan/domain"/>
		</jar>
	</target>
</project>

application.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE application PUBLIC
	"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN"
	"http://java.sun.com/dtd/application_1_3.dtd">
<application>
	<display-name>TitanCruisesExample</display-name>
	<module>
		<ejb>TravelAgentBean.jar</ejb>
	</module>
	<module>
		<ejb>Cabin.jar</ejb>
	</module>
</application>

Packaging Structure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
TitanCruises.ear
  |
  |--- META-INF
  |     |
  |     |--- application.xml
  |     |
  |     |--- MENIFEST.MF
  |
  |--- Cabin.jar
  |     |
  |     |--- META-INF 
  |     |      |
  |     |      |--- persistence.xml
  |     |      |    
  |     |      |--- MENIFEST.MF
  |     |
  |     |--- com
  |           |
  |           |--- titan
  |                  |
  |                  |--- domain
  |                         |
  |                         |--- Cabin.class
  |     
  |--- TravelAgentBean.jar
  |     |
  |     |--- META-INF 
  |     |     |
  |     |     |--- MENIFEST.MF
  |     |
  |     |--- hello
  |            |
  |            |--- titan
  |                  |
  |                  |--- travelagent
  |                         |
  |                         |
  |                         |--- TravelAgentRemote.class
  |                         |
  |                         |--- TravelAgentBean.class

Client Application: Client.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.titan.clients;
 
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import com.titan.domain.Cabin;
import com.titan.travelagent.TravelAgentRemote;
 
public class Client {
	public static void main(String[] args) {
		try {
			Context jndiContext = new InitialContext();
			Object ref = jndiContext.lookup("TitanCruises/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) {
			System.out.println("javax.naming.NamingException");
			ne.printStackTrace();
		} catch (Exception e) {
			System.out.println("Some Exception");
			e.printStackTrace();
		}
	}
}

jndi.properties

1
2
3
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=127.0.0.1:1099

Console log after deployment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
21:16:03,696 INFO  [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
21:16:03,696 INFO  [SessionFactoryObjectFactory] Unbound factory from JNDI name: persistence.units:ear=TitanCruises.ear,unitName=titan
21:16:23,953 INFO  [JBossASKernel] Created KernelDeployment for: Cabin.jar
21:16:23,953 INFO  [JBossASKernel] installing bean: persistence.units:ear=TitanCruises.ear,unitName=titan
21:16:23,953 INFO  [JBossASKernel]   with dependencies:
21:16:23,953 INFO  [JBossASKernel]   and demands:
21:16:23,953 INFO  [JBossASKernel]      jboss.jca:name=TitanDB,service=DataSourceBinding
21:16:23,953 INFO  [JBossASKernel]   and supplies:
21:16:23,953 INFO  [JBossASKernel]      persistence.units:unitName=titan
21:16:23,953 INFO  [JBossASKernel] Added bean(persistence.units:ear=TitanCruises.ear,unitName=titan) to KernelDeployment of: Cabin.jar
21:16:23,965 INFO  [STDOUT] ======> Creating interceptor metadata bridge
21:16:24,002 INFO  [JBossASKernel] Created KernelDeployment for: TravelAgentBean.jar
21:16:24,002 INFO  [JBossASKernel] installing bean: jboss.j2ee:ear=TitanCruises.ear,jar=TravelAgentBean.jar,name=TravelAgentBean,service=EJB3
21:16:24,002 INFO  [JBossASKernel]   with dependencies:
21:16:24,002 INFO  [JBossASKernel]   and demands:
21:16:24,002 INFO  [JBossASKernel]      persistence.units:ear=TitanCruises.ear,unitName=titan
21:16:24,003 INFO  [JBossASKernel]      jboss.ejb:service=EJBTimerService
21:16:24,003 INFO  [JBossASKernel]   and supplies:
21:16:24,003 INFO  [JBossASKernel]      jndi:TitanCruises/TravelAgentBean/remote
21:16:24,003 INFO  [JBossASKernel]      jndi:TravelAgentBean
21:16:24,003 INFO  [JBossASKernel]      Class:com.titan.travelagent.TravelAgentRemote
21:16:24,003 INFO  [JBossASKernel]      jndi:TitanCruises/TravelAgentBean/remote-com.titan.travelagent.TravelAgentRemote
21:16:24,003 INFO  [JBossASKernel] Added bean(jboss.j2ee:ear=TitanCruises.ear,jar=TravelAgentBean.jar,name=TravelAgentBean,service=EJB3) to KernelDeployment of: TravelAgentBean.jar
21:16:24,020 INFO  [PersistenceUnitDeployment] Starting persistence unit persistence.units:ear=TitanCruises.ear,unitName=titan
21:16:24,023 WARN  [Ejb3Configuration] Persistence provider caller does not implement the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null.
21:16:24,024 INFO  [AnnotationBinder] Binding entity from annotated class: com.titan.domain.Cabin
21:16:24,024 INFO  [EntityBinder] Bind entity com.titan.domain.Cabin on table CABIN
21:16:24,049 INFO  [ConnectionProviderFactory] Initializing connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
21:16:24,050 INFO  [InjectedDataSourceConnectionProvider] Using provided datasource
21:16:24,335 INFO  [SettingsFactory] RDBMS: PostgreSQL, version: 8.1.11
21:16:24,335 INFO  [SettingsFactory] JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.1 JDBC3 with SSL (build 412)
21:16:24,336 INFO  [Dialect] Using dialect: org.hibernate.dialect.PostgreSQLDialect
21:16:24,337 INFO  [TransactionFactoryFactory] Transaction strategy: org.hibernate.ejb.transaction.JoinableCMTTransactionFactory
21:16:24,338 INFO  [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
21:16:24,338 INFO  [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
21:16:24,338 INFO  [SettingsFactory] Automatic flush during beforeCompletion(): disabled
21:16:24,338 INFO  [SettingsFactory] Automatic session close at end of transaction: disabled
21:16:24,338 INFO  [SettingsFactory] JDBC batch size: 15
21:16:24,338 INFO  [SettingsFactory] JDBC batch updates for versioned data: disabled
21:16:24,338 INFO  [SettingsFactory] Scrollable result sets: enabled
21:16:24,339 INFO  [SettingsFactory] JDBC3 getGeneratedKeys(): disabled
21:16:24,339 INFO  [SettingsFactory] Connection release mode: auto
21:16:24,339 INFO  [SettingsFactory] Default batch fetch size: 1
21:16:24,339 INFO  [SettingsFactory] Generate SQL with comments: disabled
 
21:16:24,339 INFO  [SettingsFactory] Order SQL updates by primary key: disabled
21:16:24,339 INFO  [SettingsFactory] Order SQL inserts for batching: disabled
21:16:24,339 INFO  [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
21:16:24,340 INFO  [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
21:16:24,340 INFO  [SettingsFactory] Query language substitutions: {}
21:16:24,340 INFO  [SettingsFactory] JPA-QL strict compliance: enabled
21:16:24,340 INFO  [SettingsFactory] Second-level cache: enabled
21:16:24,340 INFO  [SettingsFactory] Query cache: disabled
21:16:24,340 INFO  [SettingsFactory] Cache region factory : org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge
21:16:24,341 INFO  [RegionFactoryCacheProviderBridge] Cache provider: org.hibernate.cache.HashtableCacheProvider
21:16:24,341 INFO  [SettingsFactory] Optimize cache for minimal puts: disabled
21:16:24,341 INFO  [SettingsFactory] Cache region prefix: TitanCruises_ear,titan
21:16:24,341 INFO  [SettingsFactory] Structured second-level cache entries: disabled
21:16:24,341 INFO  [SettingsFactory] Statistics: disabled
21:16:24,342 INFO  [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
21:16:24,342 INFO  [SettingsFactory] Default entity-mode: pojo
21:16:24,342 INFO  [SettingsFactory] Named query checking : enabled
21:16:24,351 INFO  [SessionFactoryImpl] building session factory
21:16:24,359 INFO  [SessionFactoryObjectFactory] Factory name: persistence.units:ear=TitanCruises.ear,unitName=titan
21:16:24,359 INFO  [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
21:16:24,419 INFO  [SessionFactoryObjectFactory] Bound factory to JNDI name: persistence.units:ear=TitanCruises.ear,unitName=titan
21:16:24,419 WARN  [SessionFactoryObjectFactory] InitialContext did not implement EventContext
21:16:24,422 INFO  [SchemaExport] Running hbm2ddl schema export
21:16:24,423 INFO  [SchemaExport] exporting generated schema to database
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "cabin_pkey" for table "cabin"
21:16:24,689 INFO  [SchemaExport] schema export complete
21:16:24,689 INFO  [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
21:16:24,862 INFO  [EJBContainer] STARTED EJB: com.titan.travelagent.TravelAgentBean ejbName: TravelAgentBean

Console log after running the client:

1
2
3
4
Master Suite
1
1
3

References: “Java Persistence with Hibernate” by Christian Bauer and Gavin King.
“Enterprise JavaBeans 3.0″ by Bill Burke & Richard Monson-Haefel
JBoss Documentations for EJB 3

Share This Post

2 Responses to “EJB3-Deploying Your First Beans”

  1. vezzoni Says:

    Hello Rajan!

    Congratulations! excellent post.

    One doubt: Which the packaging structure of that client app?

    cheers!

  2. admin Says:

    @vezzoni

    thanx.
    Actually the client is a simple java client. So no packaging is required. It can either run from the command prompt or from any IDE by right clicking on the client class and run it as a java client. But jndi.properties should be in the current directory.

Leave a Reply