Deploying Entity Bean on JBoss 5

Hello everyone, Now a days I am trying my hands on EJB 3.0. I have been experimenting
with EJB 3.0 on JBoss application Server. I went on many forums and found that some of
you people are facing problems deploying Entity Beans same as I did in the begining,
So i thought of writing on it n giving a working example.
I have gone through “Enterprise java beans 3.0” book by oreilly. its a very nice book.
My example is also from the same book.
I will start with the example as you people can go through the definition n all from
the book. Though you can get this code from book too m also writng it so that everything
should be in a rytdhm 😛

One more thing you can code entity bean either using annotations or by specifying the
mappings in a mapping file named “orm.xml”. I will give an example based on the mapping
file, I you guys want an example using annotations refer to the unlined link. He is my
colleague and a good friend, he has explained clearly about the example and much more
concepts of the entity beans.
http://ranjankumar.com/2008/08/11/ejb3-mapping-persistent-objects/

we will start with code now
This is our customer class : ENTITY BEAN

package com.titan.domain;
import java.io.Serializable;
/**
* @author bhupesh
*
*/
public class Customer implements Serializable {
private int id;
private String firstName;
private String lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

We Will need a session bean which will be responsible for persisting the object into
the database, though the actual work is done by an ENTITY MANAGER
SESSION BEAN :
INTERFACE –
package com.titan.travelagent;
import com.titan.domain.Customer;;
/**
* @author bhupesh
*
*/
public interface TravelAgentRemote {
public void createCustomer(Customer p);
public Customer findCustomer(int id);
}
CLASS-
package com.titan.travelagent;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.titan.domain.Customer;
/**
* @author bhupesh
*
*/
@Stateless
@Remote(TravelAgentRemote.class)
public class TravelAgentBean {
@PersistenceContext(unitName=”titan”)private EntityManager manager;
public void createCustomer(Customer cust){
manager.persist(cust);
}
public Customer findCustomer(int id){
return manager.find(Customer.class, id);
}
}

ORM Mapping file is :
<?xml version=”1.0″ encoding=”UTF-8″?>
<orm:entity-mappings version=”1.0″
xmlns:orm=”http://java.sun.com/xml/ns/persistence/orm&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd “>
<orm:description>ORM file for mapping Customer entity!</orm:description>
<orm:entity access=”PROPERTY” class=”com.titan.domain.Customer” name=”customer”>
<orm:table name=”CUSTOMER_INFO” />
<orm:attributes>
<orm:id name=”id”>
<orm:column name=”id” nullable=”false” column-definition=”integer”/>
</orm:id>
<orm:basic name=”firstName” >
<orm:column length=”20″ name=”FIRST_NAME” nullable=”false” />
</orm:basic>
<orm:basic name=”lastName” >
<orm:column length=”20″ name=”LAST_NAME” nullable=”false” />
</orm:basic>
</orm:attributes>
</orm:entity>
</orm:entity-mappings>

Now we also need a persistence.xml file which goes into the META-INF directory
here is it :

<persistence:persistence version=”1.0″
xmlns:persistence=”http://java.sun.com/xml/ns/persistence&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence persistence_1_0.xsd “>
<persistence:persistence-unit name=”titan”>
<persistence:description>
Persistence unit name is titan
</persistence:description>
<persistence:jta-data-source>java:OracleDS</persistence:jta-data-source>
<persistence:mapping-file>META-INF/orm.xml</persistence:mapping-file>
<persistence:properties>
<persistence:property name=”hibernate.hbm2ddl.auto” value=”create” />
<persistence:property name=”hibernate.dialect” value=”org.hibernate.dialect.Oracle9iDialect” />
</persistence:properties>
</persistence:persistence-unit>
</persistence:persistence>

Note one thing
<persistence:mapping-file>META-INF/orm.xml</persistence:mapping-file>
you have to give like this. Firstly I tried by giving only “orm.xml” and when I packaged &
deployed the jar it gave an error stating that the mapping file could not be found.
Then i tried another bad thing, i put this file into the src directory which ultimately came into
the bin folder. When I deployed the jar to my surprise it worked. 😛 Anyway its not a good
programming practice. So i tried giving in the above way META-INF/orm.xml and it worked fine.

JAR Structure
customer.jar
–com
—-titan
——domain
——–Customer.class
——travelagent
——–TravelAgentRemote.class
——–TravelAgaentBean.class
–META-INF
—-orm.xml
—-persistence.xml

Thats all about in how to deploy Entity Beans on JBoss 5. Wait for more to come in EJB 3.0
If you still have any queries then feel free to contact me.

Advertisements