Friday, February 26, 2010

Getting started : First EJB3 program

hello All,
I tried to explore EJB3-JPA few days back.After couple of days of debugging, searching and trial-and-error, I was able to successfully execute my first program.I would like to share this experience to other java developers.

Tools and technologies used:
MyEclipse IDE 6.5 ,EJB3,jboss-5.0.1.GA,Hibernate 3,Jdk 1.6 & Mysql 5.

Refer EJB3 tutorial by Sebastian :http://www.laliluna.de/ejb-3-tutorial-jboss.html


Now, I will mention all the steps which requires to make this tutorial example execute!

-First of all you need to make sure that your java classpath (JAVA_HOME) has been set inside MyEclipse IDE.


-Set JBOSS_HOME and and also set Jboss related values inside MyEclipse. Try to make sure that you are able to start/stop
Jboss server through MyEclipse.

-Download example source code of tutorial from site http://www.laliluna.de/ejb-3-tutorial-jboss.html, for your easy
reference.

-I used Mysql database to test this example, need to download and copy suitable mysql driver jar file and place it to
'JBOSS_HOME\default\lib'. Make sure you have created database named 'ejb3ProjectDS' as mentioned as JTA datasource inside 'persistence.xml'.

-After you set up project in MyEclipse, you need to add few more liberaries to buildpath to make sure that example works.
Create a 'lib' folder and add it to build-path. Copy following Jars to lib folder :
commons-beanutils-1.7.0.jar, commons-collections.jar, commons-digester.jar,commons-fileupload-1.2.1.jar, commons-io-1.4.jar, commons-lang-2.3.jar, commons-logging.jar, commons-validator.jar ejb3-persistence.jar, hibernate-annotations.jar hibernate-entitymanager.jar, hibernate3.jar, jakarta-poi.jar jboss-archive-browsing.jar, jboss-ejb3-ext-api.jar jboss.jar, jbossall-client.jar, log4j.jar, mysql-connector-java-5.0.8-bin.jar

(Note- you will find most of from 'JBOSS_HOME\common\lib' and rest of from apache-commons library).
Also configure you project's build-path by adding 'JBOSS_HOME\lib' and 'JBOSS_HOME\client' by adding as external class folder.

-As we are using Mysql5 database, the mentioned entity bean Id-generator is throwing error and not able to create DB table during deployment.

To resolve modify 'Book' class as follows:
change-1
---------------
@Entity @Table(name="book") @SequenceGenerator(name = "book_sequence", sequenceName = "book_id_seq") @TableGenerator( name="book_id", table="primary_keys", pkColumnName="key1", pkColumnValue="book", valueColumnName="value1") public class Book implements Serializable {
change-2
-----------------
@Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "book_id") public Integer getId() { return id; }
Keep rest of the code- as it is.

-Next step, downloaded tutorial is not having any file 'ejb-jar.xml'. So create it,it is just empty implementation.
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
-Open 'Persistence.xml' file, we need to modify it little bit as per our database details.

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> org.hibernate.ejb.HibernatePersistence java:/ejb3ProjectDS

-Now our EJB application is ready to deploy.Deploy the application as mentioned in tutorial and start the jboss server.After successful deployment,open JMX-console try to view JNDI view. You will see 'BookTestBean' inside Global JNDI Namespace.
-Connect to Mysql database through any Mysql client tool for eg. 'SQLYog'. You will see that two tables have been created within 'ejb3ProjectDS' database. - Now let's execute Client code. To execute it your need to setup hibernate.properties, jndi.properties and log4j.properties in classpath of Client class.

see sample hibernate.properties file :
hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.connection.url=jdbc:mysql://localhost:3306/ejb3ProjectDS hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.username=root hibernate.connection.password=YYYYY hibernate.show_sql="true" hibernate.archive.autodetection="class"
see sample jndi.properties file :
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces java.naming.provider.url=jnp://localhost:10099

Note- the port number inside 'java.naming.provider.url' depends on your Jboss server settings. see port number of 'jboss:service=Naming' : inside file 'bindings.xml' at path 'JBOSS_HOME\server\default\conf\bootstrap'

see sample log4j.properties file :
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.rootLogger=debug, stdout
-After all these steps the project package structure will be like as per screenshot :
-Set up run-time configuration for Client code. Include all libraries of 'JBOSS_HOME\client' in build-path of program 'FirstEJB3TutorialClient'

-Execute the Client program.

Hurray! You successfully executed your first EJB3 program !!

Friday, August 3, 2007

Hibernate Reference

Working with object-oriented software and a relational database can be cumbersome and time consuming in today's enterprise environments. Hibernate is an object/relational mapping tool for Java environments. The term object/relational mapping (ORM) refers to the technique of mapping a data representation from an object model to a relational data model with a SQL-based schema.
Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities and can significantly reduce development time otherwise spent with manual data handling in SQL and JDBC.
Hibernates goal is to relieve the developer from 95 percent of common data persistence related programming tasks.

Hibernate provides data storage and retrieval by direct mapping of Object and Relation without letting the application developer worry about the persistence logic.

Following resources helpful for understanding.

hibernate.javabeat.net/tutorials/index.php

www.roseindia.net/hibernate/index.shtml

www.developer.com/java/ent/article.php/10933_3391131_1

www.visualbuilder.com/java/hibernate/tutorial/pageorder/1/

ndpsoftware.com/HibernateMappingCheatSheet.html

Wednesday, July 18, 2007

SCJP Certification

Hello Friends, Most java developer at some stage in his career plans to get Java Certification.
As it will boost his career and also enrich his knowledge about basics of language .Basically there are very much online material available.By studying it
and practicing from this online guides ,developer will get most idea about the live online exam.Following are some of the useful links ,which I collected during my certification preparation. All the best for exam!

http://javachina.developergroup.org/
http://www.javacertificate.com/
http://www.javacamp.org/testengine/sampletester.html
http://www.revisingscjp.co.uk/
http://www.danchisholm.net/index.html
http://www.javaranch.com/mock.jsp
http://www.javacoding.net/certification/mocks
http://www.revisingscjp.co.uk/headlines/freeexams.jsp
http://www.sunguru.com/cgi-bin/java/randomquiz.cgi?
http://www.levteck.com/#Practice%20Exams
http://www.tipsmart.com/studytools/certlinks.htm#forums
http://www.jchq.net/mockexams/exam2.htm
http://bobcat.webappcabaret.net/javachina/faq/01.htm
http://directory.google.com/Top/Computers/Programming/Languages/Java/Resources/Certification/
http://www.geocities.com/online_java_certification/
http://www.angelfire.com/or/abhilash/site/quiz/quiz1.html
http://www.javabeat.net/javabeat/scjp5/index.php
http://www.javacamp.org/testengine/sampletester.html
http://www.whizlabs.com/
http://www.akgupta.com/java.htm
http://www.inesystems.com/scjp_exam.html
http://www.javaranch.com/maha/SCJP_FAQ/scjp_faq.html
http://java.boot.by/scjp-tiger/index.html
http://www.witscale.com/scjp_studynotes/introduction.html
http://java.sun.com/docs/books/tutorial/collections/intro/index.html
http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html
http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html
http://www.sun.com/training/certification/java/java_progj2se.html
http://www.danchisholm.net/july21/mybook/index.html
http://www.geocities.com/sahirshah/applets/mocktest.html
http://www.valoxo.ch/jr/mocks/mock01a.html
http://www.javabeat.net/javabeat/forum/viewtopic.php?t=269
http://rohitgeorge.netfirms.com/scjp.html
http://www.michael-thomas.com/tech/java/javacert/JCP_Keywords.htm
http://www.javadeveloper.co.in/scjp/scjp-mock-exams.html

Exam Simulator

http://www.javabeat.net/javabeat/scjp2/index.php
http://www.examulator.com/phezam/question.php
http://www.examulator.com/phezam/info.php
http://www.javaranch.com/mock.jsp

Tuesday, July 3, 2007

Java -Servlet FAQ

This FAQ series on Servlet will provide the developer quick single point guide to refresh the fundamentals of HTTP Servlet .Please follow series of FAQ at this link

Wednesday, June 20, 2007

Article on Java -J2EE technology

Vector or ArrayList- which is better and why?

See the Article on JavaWorld site


Tuesday, January 16, 2007

Welcome to the world of Java!

Hello Friends!
Welcome to the world of Java Technology!