|
|
||
Sekhar Vajjhala's Blog
«Migrate to GlassFish Activities |
Main
| Migrating WebSphere BasicCalculator example to GlassFish »
Migrating JBoss's Sample JSF-EJB3 Application To GlassFishPosted by sekhar on February 11, 2008 at 09:32 AM | Comments (6)As part of the Migrate to GlassFish acitivities, I migrated a JBoss sample to GlassFish. I am going to describe what I had to do to migrate the application. Sample ApplicationThe sample application I migrated is "Sample JSF-EJB3 Application" from JBoss Getting Started Guide. The sample can be obtained by downloading Examples Download, unzipping and cding to gettingstarted/jsfebj3 directory. To summarize the sample is a Todo application that uses JSF, facelets, Java Persistence, EJB 3 sessions beans. For more detailed documentation on the sample see Sample JSF-EJB3 Application . Refer to this documentation for additional context as you read the rest of the blog. For most part, the migration was easy. If you would like to try out the migrated sample on GlassFish,
FaceletsThe sample uses Facelets. For the purposes of this example, Facelets:
The JBoss sample bundles jsf-facelets.jar "jsfejb3.ear ! app.war#WEB-INF/lib/jsf-facelets.jar". I continued to do the same for GlassFish sample in "gf-jsfejb3.ear ! app.war # WEB-INF/lib/jsf-facelets.jar". JNDI Lookup of local ejbThe JBoss sample contains a TodoBean that uses JNDI to lookup a local ejb as follows:
public class TodoBean {
private Todo todo;
...
private TodoDaoInt getDao () {
try {
InitialContext ctx = new InitialContext();
return (TodoDaoInt) ctx.lookup("jsfejb3/TodoDao/local");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("couldn't lookup Dao", e);
}
}
@Stateless
public class TodoDao implements TodoDaoInt { ... }
public interface TodoDaoInt { ... }
The JNDI name "jsfejb3/TodoDao/local" is nonstandard. I changed it to "java:comp/env/ejb/TodoDao" and added ejb-local-ref to web.xml
// code changes to TodoBean class
InitialContext ctx = new InitialContext();
return (TodoDaoInt)ctx.lookup("java:comp/env/ejb/TodoDao");
// added to web.xml
<ejb-local-ref>
<ejb-ref-name>ejb/TodoDao</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home/>
<local>TodoDaoInt</local>
</ejb-local-ref>
Java EE 5 allows injection of entities into a managed class using dependency injection. GlassFish supports dependency injection (For JBoss support - see Sessions Beans and EJB3 Caveats). I will explore using dependency injection for migrating the sample to GlassFish at a future date. Persistence ProviderThe sample uses Hibernate as the persistence provider and HSQL database, while for GlassFish default persistence provider is Toplink and the database is Java DB . For this particular example, I chose to switch to Toplink and Java 2DB. (Other GF forum threads, blogs etc describe integration of Hibernate in GlassFish.) So here is the "jsfejb3.ear ! app.jar # META-INF/persistence.xml" from the JBoss sample:
<persistence>
<persistence-unit name="helloworld">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/DefaultDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
And here is "jsfejb3.ear ! app.jar # META-INF/persistence.xml" for GlassFish migrated sample.
<persistence>
<persistence-unit name="helloworld">
<jta-data-source>jdbc/__default</jta-data-source>
<properties>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
I did notice two differences as a result of swtiching persistence providers. Pre-population of database at deployment time: The example contains an import.sql file containing SQL statements which prepopulates the database with a list of items. For e.g.
insert into Todo (id, title, description) values (1, 'This is a title', 'The description is here!')
It appears that Hibnerate will execute the import.sql if found on classpath. Does such a similar mechanism exist in Toplink ? I looked but could not find any.I would love to hear feedback from Toplink developers. Warning message on dropping of tables: During deployment a warning message is generated. It does not impact the deployment or running of the application. I started an email thread on this. But my analysis so far. In the following entity class:
@Entity
public class Todo implements Serializable {
...
@Id @GeneratedValue
public long getId() { return id;}
...
}
the @GeneratedValue defaults to @GeneratedValue.AUTO which leaves the primary key mapping to the persistence provider. And in Toplink seems to generate a Table for @GeneratedValue.AUTO . When the sample application is deployed, the actual data tables are dropped but not the table generated for the primary key. Hence the warning message. However, as I said, it does not prevent the application from working. Packaging changesHere is a summary of packaging changes I made to JBoss ear (by modifying build.xml).
As always, feedback is welcome. For questions and feedback, visit Migrate To GlassFish project and follow the "Discussion Forums" link . Bookmark blog post: CommentsComments are listed in date ascending order (oldest first) | Post Comment
| ||
|
|