EJB Sample Application

Last modified by superadmin on 2018-01-12 20:28

EJB Sample Application

Setup of the Database Connection

  1. Install TcpView
  2. Create tables in a database - see Create DB Schema Manually
  3. Make an application and a project
    1a. In JDeveloper menu pick File -> New -> General -> Application. Pick name="MCTestingApp", leave Application Template to "No Template". Press [OK] button.
    1a. Create a project with name EJBProject (if prompted, pick "Empty Project"). 
  4. Find the port the Oracle XE is listening to.
    1a. If you are optimistic about your Oracle XE Installation using the default port, pick value 1521. Otherwise, you can determine the port via these activities:
    1a. Log to Oracle XE console (if necessary, start the OracleServiceXE manually using the Start -> Control Panel -> Administrative Tools -> Services.
    1a. Run the TcpView application. You should see the following screen:

Unknown macro: picture.
Figure: TcpView ekrāns Port number is indicated besides the oracle.exe. In the picture Oracle XE port number is 3156. 

  1. Make a connection to a database
    1a. Right click "Connections" under the tab "Application Resources" in the "Application Navigator" view:

Unknown macro: picture.
Figure: Creating a new DB Connection

1a. Type in values (use whatever password you created in the manual Oracle XE Setup step:

Unknown macro: picture.
Figure: Editing values of DB Connection

1a. Press "Test Connection" button and you should see "Success!" after some time. Close the dialog with "OK". 

Generating Entities

  1. Right click the project name and pick New -> Business Tier -> EJB -> Entities from Tables.
    1a. Pick "EJB 3.0" radio button, press "Next"
    1a. Beside the "Persistence unit" click "New"; write Name="mctesting", Database Platform = "Oracle", Server Platform = "OC4J_11_1_1". Leave JTA Datasource name empty and set Non-JTA Datasource name = "mctesting". Press "Next"
    1a. Pick "Online database connection", press "Next"
    1a. Leave the default value "MyOracleXEConnection", do not configure any offline database, press "Next"
    1a. Leave the "%" sign, press button "Query"
    1a. Pick all the tables under the database and press ">>" (Add all), press "Next"
    1a. Leave the default package name "ejbproject", leave "Serializable" checkmark and "java.util.List" for relationship fields. Leave the "place annotation on fields".
    1a. Pick "MCTESTING.MCUSER" and select entity name "User" instead of "Mcuser" (in Java "User" is not a reserved word). Press "Next" and "Finish". 
  2. View the generated entity classes in package "ejbproject". There should be 4 files generated - Alternative.java, Assignment.java, Question.java, User.java. Find annotations @Entity, @Id, @Column, @ManyToOne, @OneToMany, @JoinColumn and @NamedQuery. The entity class User also has @Table annotation, because the database table name does not match the class name.
  3. Add the @GeneratedValue annotations to indicate that unique IDs should be generated from the existing DB sequences:
    1a. Display all the sequences in the "mctesting" schema: 
select object_name from user_objects where object_type='SEQUENCE';

 This command should display the following: 

OBJECT_NAME
-------------------
SEQ_MCUSER_ID
SEQ_ASSIGNMENT_ID
SEQ_QUESTION_ID
SEQ_ALTERNATIVE_ID

 If it does not, check, if you have initialized the sequences properly as described in the Setup notes.

  1. Edit the code of Question class as follows: 
public class Question implements Serializable {
    private char[] content;
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE,
        generator="SEQ_QUESTION_ID")
    @Column(name="QUESTION_ID", nullable = false)
    private Long questionId;

 (I.e. just insert the missing "GeneratedValue" line anywhere before the "questionId" declaration.) Here @GeneratedValue indicates that "questionId" should be populated from a database sequence SEQ_QUESTION_ID. Similarly edit the other 3 classes. You may need to import a few annotation classes (namely javax.persistence.GeneratedValue and javax.persistence.GenerationType - just press the Alt+Enter, if you get any errors due to missing imports. 

  1. Right-click the project "EJBProject" and create a "New" -> "EJB" -> "EJB Diagram". Leave the default name "EJB Diagram1" and default package "ejbproject". Click "OK". 
  2. Select all the Java entity classes (Alternative.java, Assignment.java, Question.java, User.java) and drag them into the diagram. It should create an UML class diagram. 
  3. Save your work. 

Add a few more queries

  1. Edit the code for class User
@Entity
@NamedQueries({
@NamedQuery(name = "User.findAll", query = "select o from User o"),
@NamedQuery(name="User.findById",query="select o FROM User o WHERE o.mcuserId = :userid")
})
@Table(name = "MCUSER")
public class User implements Serializable {
...

 I.e. you create another query "User.findById" beside the automatically generated query. If there are multiple queries for the same class, they all go under the annotation NamedQueries

  1. Right-click the project "EJBProject" and pick "Make". Check that the code compiles and does not generate any error messages. 

Adding Business Logic

  1. Right click the "EJBProject" and pick New -> Business Tier -> EJB -> Session Bean
  2. Pick the "EJB Name" as "MCAppFacade".
  3. Expand "User" node and check that "findById" method (it corresponds to the newly added named query) is also there. Press "Next". 
  4. Pick "Bean Class" as "ejbproject.buslogic.MCAppFacadeBean" and press "Next". 
  5. Leave both checkboxes (Local and Remote interfaces) checked, press "Next" and "Finish". 
  6. Add one more method to the class MCAppFacadeBean with the code editor: 
public String getEmail(long userid) {
    User u =
    (User)this.em.createNamedQuery("User.findById").
      setParameter("userid",userid).getSingleResult();
    return u.getEmail();
}
  1. Recompile project - right click and "Make". 

Bibliography

  1. http://www.oracle.com/technology/obe/obe1013jdev/10131/10131_ejb_30/ejb_30.htm
  2. http://www.oracle.com/technology/obe/obe11jdev/11/ejb/ejb.html - Creating EJB from JDeveloper11
  3. http://www.oracle.com/technology/tech/java/ejb30.html - See the link: "EJB 3 In Action : Code Examples for Oracle Application Sever 10g download zip".
  4. See the book http://www.ante.lv/downloads/Manning.EJB.3.in.Action.Apr.2007.pdf.
  5. http://www.oracle.com/technology/products/ias/toplink/jpa/tutorials/jsf-jpa-tutorial.html - JPA and GlassFish tutorial (shows how to configure "persistence.xml").
  6. http://javanotepad.blogspot.com/2007/07/working-with-non-jta-datasource-in.html - notes about non-JTA datasources.
Tags:
Created by Kalvis Apsītis on 2008-03-16 10:40
    
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 6.4 - Documentation