Post Subject : Tutorial on JBossIDE EJB3.0 Tools

Ref. 1 : JBoss IDE TrailBlazer : Hike 1 - EJB 3.0 Tools
         http://trailblazer.demo.jboss.com/IDETrail/

=== Environment in use;
- Windows XP SP2
- JDK 1.5.0_05
- JBoss AS 4.0.3SP1
- JBoss IDE1.5RC1 + Eclipse 3.1.1

Following notes/findings cover only Task 5 below;

Task 1: Download and install JDK 1.5
Task 2: Install JBoss AS 4.0.3SP1 using the installer
Task 3: Download and install JBoss IDE1.5RC1 and Eclipse3.1.1
Task 4: Configure Eclipse and JBossIDE to control JBoss AS
Task 5: Carry out a tutorial (Ref. 1)

- Ref. 1 contains following trails;

Trail 1: Create an EJB 3.0 Project
Trail 2: Create EJB 3.0 Entity Beans
Trail 3: Create EJB 3.0 Session Beans
Trail 4: Create a Java Servlet
Trail 5: Package an EJB 3.0 Application
Trail 6: Deploy and Debug

Trail 1: Create an EJB 3.0 Project
- follow the trail in Ref. 1
= jump over "Create a JBoss configuration" link since it has been configured at 
Task 4 in the last post of this thread.
- on completion of the project named "articles", confirm following point before 
proceeding further steps.
= in the Eclipse Package Explorer, the JRE System Library path is to be "[jdk 
1.5]", not "(C:\)"[jre 1.5.0_05]" (see Task 1. in the last post)
= if not, change the library location as follow;
= right-click the project "articles" in the Package Explorer.
= then, go to "Properties > Java Build Path > Libraries > JRE System Library > 
Edit... > Installed JREs... > Edit..."
= in "Edit JRE" box;
* JRE name --> jdk 1.5
* JRE home directory --> C:\jdk1.5.0_05\jre
= click "OK"...

Trail 2: Create EJB 3.0 Entity Beans
- create "Article.java" and "Author.java" as per the trail in Ref. 1
- type "@E" then "Ctrl+space" for the code assist, a pop-up menu shows a list 
of annotations.
- select "@Entity"

= note1: If no "@Entity",
* right-click the project "articles" in the Eclipse Package Explorer (left top).
* then go to "Properties > Java Build Path > Libraries (tag) > Add External 
JARs..."
* add "ejb3-persistence.jar" under "..\server\all\deploy\ejb3.deployer".

= note2: Creation of the class "Author" (in Article.java)
* place the mouse cursor on "Author", then "Ctrl+1" (or right-click 
"light-bulb") to show "Quick Fix"

= note3: "Organize Imports" for "Collection" (in Author.java)
* place the mouse cursor on "Collection"
* then "Ctrl+Shift+O" to show "Organize Imports"
* (or right-click "light-bulb", then Quick Fix) 

Trail 3: Create EJB 3.0 Session Beans
- create "Authors.java" and "AuthorsBean.java" as per the trail in Ref. 1
- in "AuthorsBean.java", use "@PersistenceContext" instead of "@Inject".
Ref.: "A problem with @Injected EntityManager"
http://www.jboss.org/index.html?module=bb&op=viewtopic&t=66949

Trail 4: Create a Java Servlet
- add "J2EE 1.4Libraries (JBoss-IDE)" and
- create "AuthorServlet.java" as per the trail in Ref. 1

= note1: On completion of the servlet, descriptions in "Problems" tag in the 
Eclipse workbench might show;
Problem : "The serializable class AuthorServlet does not declare a static final 
serialVersionUID field of type long"
  | AuthorServlet.java  articles/src/org/jboss/ejb3demo/web     line xx
* if the problem occur,
* place mouse cursor on "AuthorServlet", then "Ctl+1".
* select and double-click "Add default serial version ID"
* a set of codes below is added at the top and the problem will be gone.
/**
  |  * 
  |  */
  | private static final long serialVersionUID = 1L;
= note2: Apply same process as Note1 to following classes, if necessary;
* class "Article" in "Article.java"
* class "Author" in "Author.java"

Trail 5: Package an EJB 3.0 Application
- as per the steps in Ref. 1

Trail 6: Deploy and Debug
- start "Debug" session to start JBoss AS and deploy .ejb3 and .war files as 
per the steps in Ref. 1
- go to URL "http://localhost:8080/authors/authors"; and see the results.
= they might be as follows (error);
HTTP Status 500 - 
  | 
-----------------------------------------------------------------------------------------
  | type Exception report
  | 
  | message 
  | description The server encountered an internal error () that prevented it 
from fulfilling 
  | this request.
  | 
  | exception 
  | java.lang.reflect.UndeclaredThrowableException
  |     $Proxy83.addAuthor(Unknown Source)
  |     
org.jboss.ejb3demo.web.AuthorServlet.createArticles(AuthorServlet.java:57)
  |     org.jboss.ejb3demo.web.AuthorServlet.doPost(AuthorServlet.java:50)
  |     javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  |     javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
  |     
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
  | 
  | note The full stack trace of the root cause is available in the Apache 
Tomcat/5.5 logs.
  | 
------------------------------------------------------------------------------------------
  | Apache Tomcat/5.5
- if so, modify "Articles.java" and "Author.java" (not "Authors.java")

Ref.: "JBoss EJB3.0 tutorial problems and solutions"
http://www.jboss.org/index.html?module=bb&op=viewtopic&t=69256
In "Articles.java";
  | add    : import java.io.Serializable;
  | change : public class Article {... --> public class Article implements 
Serializable {...
  | 
  | In "Author.java";
  | add    : import java.io.Serializable;
  | change : public class Author {... --> public class Author implements 
Serializable {...
- then, re-packaging and re-deploy them.

= note1: re-deploy both of .ejb3 and .war files even if revision is one side 
only, otherwise the results might not come properly.
= note2: a few repetition might be necessary to get the results. Repeat 
un-deploy/re-packaging/re-deploy if no success.

= In "server.log" file, following part seems to be a set of characteristic 
messages of ejb3/hibernate in case of normal process.
  :
  | 2005-11-06 15:31:46,440 DEBUG 
[org.jboss.ejb3.entity.ManagedEntityManagerFactory] GETTING NEW ...
  | 2005-11-06 15:31:46,440 DEBUG 
[org.jboss.ejb3.entity.ManagedEntityManagerFactory] logLived...
  | 2005-11-06 15:31:46,440 DEBUG 
[org.jboss.ejb3.entity.ManagedEntityManagerFactory] LONG LIVED ...
  | 2005-11-06 15:31:46,440 DEBUG [org.hibernate.impl.SessionImpl] opened 
session at timestamp: ...
  | 2005-11-06 15:31:46,440 DEBUG [org.hibernate.impl.SessionImpl] find: from 
Author
  | 2005-11-06 15:31:46,440 DEBUG [org.hibernate.engine.QueryParameters] named 
parameters: {}
  | 2005-11-06 15:31:46,440 DEBUG [org.hibernate.hql.ast.QueryTranslatorImpl] 
parse() - HQL: from ...
  | 2005-11-06 15:31:46,450 DEBUG [org.hibernate.hql.ast.AST] --- HQL AST ---
  |  \-[QUERY] 'query'
  |     \-[SELECT_FROM] 'SELECT_FROM'
  |        \-[FROM] 'from'
  |           \-[RANGE] 'RANGE'
  |              \-[DOT] '.'
  |                 +-[DOT] '.'
  |                 |  +-[DOT] '.'
  |                 |  |  +-[IDENT] 'org'
  |                 |  |  \-[IDENT] 'jboss'
  |                 |  \-[IDENT] 'ejb3demo'
  |                 \-[IDENT] 'Author'
  | 
  | 2005-11-06 15:31:46,450 DEBUG [org.hibernate.hql.ast.ErrorCounter] 
throwQueryException() : ...
  | 2005-11-06 15:31:46,460 DEBUG [org.hibernate.hql.antlr.HqlSqlBaseWalker] 
select << begin ...
  | 2005-11-06 15:31:46,460 DEBUG [org.hibernate.hql.ast.tree.FromElement] 
FromClause{level=1} ...
  | 2005-11-06 15:31:46,460 DEBUG [org.hibernate.hql.antlr.HqlSqlBaseWalker] 
select : finishing ...
  | 2005-11-06 15:31:46,460 DEBUG [org.hibernate.hql.ast.HqlSqlWalker] 
processQuery() :  ( SELECT ...
  | 2005-11-06 15:31:46,460 DEBUG [org.hibernate.hql.ast.HqlSqlWalker] Derived 
SELECT clause created.
  | 2005-11-06 15:31:46,460 DEBUG [org.hibernate.hql.ast.util.JoinProcessor] 
Using FROM fragment ...
  | 2005-11-06 15:31:46,460 DEBUG [org.hibernate.hql.antlr.HqlSqlBaseWalker] 
select >> end ...
  | 2005-11-06 15:31:46,470 DEBUG [org.hibernate.hql.ast.AST] --- SQL AST ---
  |  \-[SELECT] QueryNode: 'SELECT'  querySpaces (AUTHORS)
  |     +-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
  |     |  +-[SELECT_EXPR] SelectExpressionImpl: 'author0_.authorId as 
authorId9_' {FromElement{...
  |     |  \-[SQL_TOKEN] SqlFragment: 'author0_.name as name9_'
  |     \-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, 
fromElements=1, ...
  |        \-[FROM_FRAGMENT] FromElement: 'AUTHORS author0_' 
FromElement{explicit,not a collection ...
  | 
  | 2005-11-06 15:31:46,470 DEBUG [org.hibernate.hql.ast.ErrorCounter] 
throwQueryException() : ...
  | 2005-11-06 15:31:46,470 DEBUG [org.hibernate.hql.ast.QueryTranslatorImpl] 
HQL: from org.jboss...
  | 2005-11-06 15:31:46,470 DEBUG [org.hibernate.hql.ast.QueryTranslatorImpl] 
SQL: select author0_...
  | 2005-11-06 15:31:46,470 DEBUG [org.hibernate.hql.ast.ErrorCounter] 
throwQueryException() : ...
  |   :
= in "HSQL Database Manager", type "select * from ARTICLES" to confirm the 
records.
ARTICLEID  TITLE                     BODY                   AUTHORID
  | --------------------------------------------------------------------
  |     1      marshall's 1st article    this is an article        1
  |     2      marshall's 2nd article    this is also article      2
  |     3      max's 1st article         this is an article        3
  |     4      max's 2nd article         this is also article      4
= for "AUTHORS";
AUTHORID   NAME
  | -------------------
  |     1      Marshall
  |     2      Marshall
  |     3      Max
  |     4      Max

=== Ref. On completion of the trails, the files are as follow;
Article.java 
package org.jboss.ejb3demo;
  | 
  | import java.io.Serializable;
  | 
  | import javax.persistence.Column;
  | import javax.persistence.Entity;
  | import javax.persistence.GeneratorType;
  | import javax.persistence.Id;
  | import javax.persistence.JoinColumn;
  | import javax.persistence.ManyToOne;
  | import javax.persistence.Table;
  | 
  | @Entity
  | @Table(name = "ARTICLES")
  | public class Article implements Serializable {
  | 
  |     /**
  |      * 
  |      */
  |     private static final long serialVersionUID = 1L;
  |     
  |     private int articleId;
  |     private String title;
  |     private String body;
  |     private Author author;
  |     
  |     @Id (generate = GeneratorType.AUTO)
  |     @Column (name = "articleId")
  |     public int getArticleId() {
  |             return articleId;
  |     }
  |     public void setArticleId(int articleId) {
  |             this.articleId = articleId;
  |     }
  |     
  |     @ManyToOne
  |     @JoinColumn (name = "authorId")
  |     public Author getAuthor() {
  |             return author;
  |     }
  |     public void setAuthor(Author author) {
  |             this.author = author;
  |     }
  |     public String getBody() {
  |             return body;
  |     }
  |     public void setBody(String body) {
  |             this.body = body;
  |     }
  |     public String getTitle() {
  |             return title;
  |     }
  |     public void setTitle(String title) {
  |             this.title = title;
  |     }
  | }
Authors.java
package org.jboss.ejb3demo;
  | 
  | import java.util.List;
  | import javax.ejb.Remote;
  | 
  | @Remote
  | public interface Authors {
  |     public List<Author> getAllAuthors();
  |     public Author addAuthor (String name);
  |     public void addArticle (Author author, String title, String body);
  | }
AuthorsBean.java
package org.jboss.ejb3demo;
  | 
  | import javax.ejb.Stateless;
  | 
  | import java.util.ArrayList;
  | import java.util.List;
  | 
  | import javax.persistence.EntityManager;
  | import javax.persistence.PersistenceContext;
  | import javax.persistence.Query;
  | 
  | 
  | public @Stateless class AuthorsBean implements Authors {
  | 
  |     @PersistenceContext
  |     private EntityManager manager;
  |     
  |     public void addArticle(Author author, String title, String body) {
  |             manager.persist(author);
  |             author.addArticle(title, body);
  |     }
  | 
  |     public Author addAuthor(String name) {
  |             Author author = new Author();
  |             author.setName(name);
  |             return author;
  |     }
  | 
  |     public List<Author> getAllAuthors() {
  |             ArrayList<Author> authors = new ArrayList<Author>();
  |             Query q = manager.createQuery("from Author");
  |             
  |             for (Object o : q.getResultList())
  |             {
  |                     authors.add((Author) o);
  |             }
  |             return authors;
  |     }
  | }
Author.java
package org.jboss.ejb3demo;
  | 
  | import java.io.Serializable;
  | import java.util.ArrayList;
  | import java.util.Collection;
  | 
  | import javax.persistence.CascadeType;
  | import javax.persistence.Column;
  | import javax.persistence.Entity;
  | import javax.persistence.FetchType;
  | import javax.persistence.GeneratorType;
  | import javax.persistence.Id;
  | import javax.persistence.JoinColumn;
  | import javax.persistence.OneToMany;
  | import javax.persistence.Table;
  | 
  | @Entity
  | @Table(name = "AUTHORS")
  | public class Author implements Serializable {
  | 
  |     /**
  |      * 
  |      */
  |     private static final long serialVersionUID = 1L;
  |     
  |     private int authorId;
  |     private String name;
  |     private Collection<Article> articles;
  |     
  |     @OneToMany (cascade = CascadeType.ALL, fetch = FetchType.EAGER, 
mappedBy = "author")
  |     @JoinColumn (name = "authorId")
  |     public Collection<Article> getArticles() {
  |             return articles;
  |     }
  | 
  |     public void setArticles(Collection<Article> articles) {
  |             this.articles = articles;
  |     }
  |     
  |     @Id(generate = GeneratorType.AUTO)
  |     @Column (name = "authorId")
  |     public int getAuthorId() {
  |             return authorId;
  |     }
  | 
  |     public void setAuthorId(int authorId) {
  |             this.authorId = authorId;
  |     }
  | 
  |     public String getName() {
  |             return name;
  |     }
  | 
  |     public void setName(String name) {
  |             this.name = name;
  |     }
  |     
  |     public void addArticle(String title, String body) {
  |             if (articles == null)
  |                     articles = new ArrayList<Article>();
  |             Article article = new Article();
  |             article.setAuthor(this);
  |             article.setBody(body);
  |             article.setTitle(title);
  |             articles.add(article);
  |     }
  | }
AuthorServlet.java
package org.jboss.ejb3demo.web;
  | 
  | import java.io.IOException;
  | import java.io.PrintWriter;
  | 
  | import javax.naming.Context;
  | import javax.naming.InitialContext;
  | import javax.naming.NamingException;
  | import javax.servlet.ServletException;
  | import javax.servlet.http.HttpServlet;
  | import javax.servlet.http.HttpServletRequest;
  | import javax.servlet.http.HttpServletResponse;
  | 
  | import org.jboss.ejb3demo.Article;
  | import org.jboss.ejb3demo.Author;
  | import org.jboss.ejb3demo.Authors;
  | 
  | public class AuthorServlet extends HttpServlet {
  | 
  |     /**
  |      * 
  |      */
  |     private static final long serialVersionUID = 1L;
  |     private Authors authorsBean;
  |     
  |     public void init() throws ServletException {
  |             try {
  |                     Context context = new InitialContext();
  |                     authorsBean =(Authors) 
context.lookup(Authors.class.getName());
  |             } catch (NamingException e) {
  |                     e.printStackTrace();
  |       }
  |     }
  |     
  |     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  |         throws ServletException, IOException {
  |             doPost(req, resp);
  |     }
  |     
  |     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  |         throws ServletException, IOException {
  |             String mode = req.getParameter("mode");
  | 
  |             if (mode == null)
  |                     mode = "show";
  |             if (mode.equals("show"))
  |             {
  |                     showArticles(req, resp);
  |             }
  |             else if (mode.equals("create"))
  |             {
  |                     createArticles(req, resp);
  |             }
  |     }
  | 
  |     private void createArticles(HttpServletRequest req, HttpServletResponse 
resp)
  |             throws ServletException, IOException
  |     {
  |             Author marshall = authorsBean.addAuthor("Marshall");
  |             Author max = authorsBean.addAuthor("Max");
  |             
  |             authorsBean.addArticle(marshall, "marshall's 1st article", 
"this is an article");
  |             authorsBean.addArticle(marshall, "marshall's 2nd article", 
"this is also an article");
  |             authorsBean.addArticle(max,      "max's 1st article", "this is 
an article");
  |             authorsBean.addArticle(max,      "max's 2nd article", "this is 
also an article");
  | 
  |             showArticles(req, resp);
  |     }
  | 
  |     private void showArticles(HttpServletRequest req, HttpServletResponse 
resp)
  |             throws ServletException, IOException    
  |     {
  |             PrintWriter out = resp.getWriter();
  |             
  |             for (Author author : authorsBean.getAllAuthors())
  |             {
  |                     for (Article article : author.getArticles())
  |                     {
  |                             out.println("<b>" + article.getTitle() + "</b>" 
+ " by <i>" + author.getName() + "</i><br/>");
  |                             out.println("    " + article.getBody() + 
"<br/><br/>");
  |                     }
  |             }
  |             out.println("<form method=\"POST\" action=\"authors\">");
  |             out.println("<input type=\"hidden\" name=\"mode\" 
value=\"create\">");
  |             out.println("<input type=\"submit\" value=\"Create 
articles\">");
  |             out.println("</form>");
  |     }
  | }
web.xml
<?xml version="1.0" encoding="UTF-8"?>
  | <web-app version="2.4"
  |     xmlns="http://java.sun.com/xml/ns/j2ee";
  |     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
  |     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd";>
  |     <servlet>
  |             <servlet-name>AuthorServlet</servlet-name>
  |             
<servlet-class>org.jboss.ejb3demo.web.AuthorServlet</servlet-class>
  |     </servlet>
  |     <servlet-mapping>
  |             <servlet-name>AuthorServlet</servlet-name>
  |             <url-pattern>/authors</url-pattern>
  |     </servlet-mapping>
  | </web-app>
packaging-build.xml
<?xml version="1.0" encoding="UTF-8"?>
  | <project name="Packaging Generator" default="_packaging_generation_">
  | <target name="_packaging_generation_" depends="N65540,N65557"/>
  | <target name="N65540" description="authors-beans.ejb3">
  | <jar destfile="authors-beans.ejb3">
  | <zipfileset dir="bin" excludes="**/*Servlet.java"/>
  | </jar>
  | </target>
  | <target name="N65557" description="authors.war">
  | <jar destfile="authors.war">
  | <zipfileset dir="src" prefix="WEB-INF">
  | <include name="web.xml"/>
  | </zipfileset>
  | <zipfileset dir="bin" prefix="WEB-INF/classes" includes="**/*Servlet.java"/>
  | </jar>
  | </target>
  | </project>

jbosszone


View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3905649#3905649

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3905649


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
JBoss-user mailing list
JBoss-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to