It is currently Fri May 18, 2012 4:08 pm


Post a new topicPost a reply Page 1 of 1   [ 1 post ]
Author Message
 Post subject: Enterprise Java Beans 3 Tutorial Part2
PostPosted: Mon Sep 13, 2010 2:00 pm 
Site Admin

Joined: Sat Aug 02, 2008 8:00 am
Posts: 147
Creating Your First Entity Bean

As we mentioned earlier entity beans map to a record in the database. In EJB3 entity beans are created by marking the bean class with the Entity annotation. For the example that we have here we will have an entity bean called book.
Preparation for creating the JPA Project
Creating the My SQL data source on JBoss Application Server
In order to be able to run our entity beans on the application server we need to configure a data source on the server for the server to be able to create connections to the data base server. Creating data sources in Jboss is quite simple, all what you will have to do is to make a copy of the mysql-ds.xml file located in the examples folder under the following path in your Jboss folder
Jboss server directory\docs\examples\jca
Into the following folder
Jboss server directory \server\default\deploy
And then you will need to edit this file to match the configuration and the location of your database system, Here is how my configuration file looks like.

Code:
<?xml version="1.0" encoding="UTF-8"?>

<datasources>
  <local-tx-datasource>
    <jndi-name>MySqlDS</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/University</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>root</user-name>
    <password>admin</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
    <metadata>
       <type-mapping>mySQL</type-mapping>
    </metadata>
  </local-tx-datasource>
</datasources>



After configuring the mysql-ds.xml restart the application server for your configuration to take place.
Creating a connection to MySQL database in Eclipse EE
From window>view open the Data Source Explorer window
In the Data Source Explorer window right click “Database Connections” and choose new

Image

In the “Connection Profile” window choose MySQL as the connection profile
Image

In the “Driver and Connection Details” window select the MySQL driver and enter the connection data as in the following figure.

Image

Click “Test Connection” to make sure the connection is correct and then click finish.
Note: If you did not configure the driver you can configure a new driver for your version of MySQL in Eclipse EE by clicking the new driver button on the right side of the drivers list.
To configure the driver you need to download the latest MySQL connector Jar from the following location.
http://www.mysql.com/products/connector


Create the Books table in the database using the following script
Code:
CREATE TABLE `books` (
  `BookID` int(11) NOT NULL AUTO_INCREMENT,
  `Title` varchar(100) DEFAULT NULL,
  `Year` int(11) DEFAULT NULL,
  `TotalNumberOfCopies` int(11) DEFAULT NULL,
  `CopiesOnShelf` int(11) DEFAULT NULL,
  PRIMARY KEY (`BookID`)
)


Creating the JPA Project
The first thing we will need to do is to create a new EJB project in Eclipse EE the entity beans are created in a Java Persistence API project (JPA)
On the project explorer panel right click then choose new then choose project
In the list of projects choose JPA project as follows

Image


Note: Make sure you select the runtime environment to be Jboss and to configure this environment in the Java runtime environments in the preferences menu option before you create the project.


Enter the name of the project (I used EJB3Tutorial) and select the JBoss in the Target runtime section

Image


Click “Next” and then click “Next” to leave the default source folder
In the JPA Facet screen select the connection to the MySQL database that you created earlier. This connection will be used to connect to the server and create the objects from the database and then click finish.

Image

After creating the JPA Project we need to create the ORM objects from the table that we already created in the database. To do that right click on the newly created project and choose ”JPA Tools” then “Generate Entities From Tables

Image


In the “Select Tables” window choose the connection to MySQL database and the schema name from the dropdown list. This will show the list of tables in the schema. From the list of tables select the books table and click “Next”.

Image



In the “Table Associations” window click “Next
In the “Customize Default Entry generation” window select auto for “Key Generation” because this is an auto increment table in MySQL which will be increased automatically. Enter “com.thejavacode.tutorials.ejb.entity” as the package for the newly generated class.

Image



In the “Customize Individual Fields” window click “Next” and then click “Finish

Your Entity class will be created and it looks as follows

Code:
package com.thejavacode.tutorials.ejb.entity;

import java.io.Serializable;
import javax.persistence.*;


/**
* The persistent class for the books database table.
*
*/
@Entity
@Table(name="books")
public class Book implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name="BookID")
   private int bookID;

   @Column(name="CopiesOnShelf")
   private int copiesOnShelf;

   @Column(name="Title")
   private String title;

   @Column(name="TotalNumberOfCopies")
   private int totalNumberOfCopies;

   @Column(name="Year")
   private int year;

    public Book() {
    }

   public int getBookID() {
      return this.bookID;
   }

   public void setBookID(int bookID) {
      this.bookID = bookID;
   }

   public int getCopiesOnShelf() {
      return this.copiesOnShelf;
   }

   public void setCopiesOnShelf(int copiesOnShelf) {
      this.copiesOnShelf = copiesOnShelf;
   }

   public String getTitle() {
      return this.title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   public int getTotalNumberOfCopies() {
      return this.totalNumberOfCopies;
   }

   public void setTotalNumberOfCopies(int totalNumberOfCopies) {
      this.totalNumberOfCopies = totalNumberOfCopies;
   }

   public int getYear() {
      return this.year;
   }

   public void setYear(int year) {
      this.year = year;
   }

}


In the next part I am going to demonstrate how you can use these entity beans in a newly created session bean.


You do not have the required permissions to view the files attached to this post.

_________________

Muhammad Safwat Fuad
The Java Code Admin
Java Technical Lead.
Mobile: +2010-2942-538
Email:mtv134@yahoo.com


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 1 post ]


Who is online

Registered users: MSN [Bot], RepImpedhex


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
twilightBB Style by Daniel St. Jules of Gamexe.net

[
SEO MOD © 2007 StarTrekGuide ]