Wednesday Nov 21, 2012

JPA 2.1 Schema Generation (TOTD #187)

UPDATED: May 24, 2012

JPA 2.1 specification is now final and can be download from here. The Reference Implementation is integrated in GlassFish 4 Promoted Builds (soon to be final). The Schema Generation property names have changed since the blog was originally published and is now updated in the table below.

Blog from Nov 11, 2012

This blog explained some of the key features of JPA 2.1 earlier. Since then Schema Generation has been added to JPA 2.1. This Tip Of The Day (TOTD) will provide more details about this new feature in JPA 2.1.

Schema Generation refers to generation of database artifacts like tables, indexes, and constraints in a database schema. It may or may not involve generation of a proper database schema depending upon the credentials and authorization of the user. This helps in prototyping of your application where the required artifacts are generated either prior to application deployment or as part of EntityManagerFactory creation. This is also useful in environments that require provisioning database on demand, e.g. in a cloud.

This feature will allow your JPA domain object model to be directly generated in a database. The generated schema may need to be tuned for actual production environment. This usecase is supported by allowing the schema generation to occur into DDL scripts which can then be further tuned by a DBA.

The following set of properties in persistence.xml or specified during EntityManagerFactory creation controls the behaviour of schema generation.

Property Name
Purpose
Values
javax.persistence.schema-generation.database.action
Specifies the action to be taken by the persistence provider with regard to the database artifacts Per.book "none", "create", "drop-and-create", "drop"
javax.persistence.schema-generation.scripts.action
Specifies which scripts are to be generated by the persistence provider Per.book "none", "create", "drop-and-create", "drop"
javax.persistence.schema-generation.create-source
javax.persistence.schema-generation.drop-source
Specifies whether the creation/dropping of database artifacts is to occur on the basis of the object/relational mapping metadata, DDL script, or a combination of the two.
"metadata", "script", "metadata-then-script", "script-then-metadata"
javax.persistence.schema-generation.create-database-schemas
Specifies whether the persistence provider is to create the database schema(s) in addi- tion to creating database objects such as tables, sequences, constraints, etc.
"true", "false"
javax.persistence.schema-generation.scripts.create-target
javax.persistence.schema-generation.scripts.drop-target
If scripts are to be generated, controls target locations for writing of scripts. Writers are pre-configured for the persistence provider. Need to be specified only if scripts are to be generated.
java.io.Writer (e.g. MyWriter.class) or URL strings
javax.persistence.database-product-name,
javax.persistence.database-major-version, javax.persistence.database-minor-version
Needed if scripts are to be generated and no connection to target database. Values are those obtained from JDBC DatabaseMetaData.

javax.persistence.schema-generation.scripts.create-script-source
javax.persistence.schema-generation.scripts.drop-script-source
Specifies locations from which DDL scripts are to be read. Readers are pre-configured for the persistence provider.
java.io.Reader (e.g. MyReader.class) or URL strings
javax.persistence.schema-generation.connection
JDBC connection to be used for schema generation

javax.persistence.sql-load-script-source
Specifies location of SQL bulk load script.
java.io.Reader (e.g. MyReader.class) or URL string

Section 11.2 in the JPA 2.1 specification defines the annotations used for schema generation process. For example, @Table, @Column, @CollectionTable, @JoinTable, @JoinColumn, are used to define the generated schema. Several layers of defaulting may be involved. For example, the table name is defaulted from entity name and entity name (which can be specified explicitly as well) is defaulted from the class name. However annotations may be used to override or customize the values.

The following entity class:

@Entity public class Employee {
    @Id private int id;
    private String name;
    . . .
    @ManyToOne     private Department dept; }

is generated in the database with the following attributes:

  • Maps to EMPLOYEE table in default schema
  • "id" field is mapped to ID column as primary key
  • "name" is mapped to NAME column with a default VARCHAR(255). The length of this field can be easily tuned using @Column.
  • @ManyToOne is mapped to DEPT_ID foreign key column. Can be customized using JOIN_COLUMN.

In addition to these properties, couple of new annotations are added to JPA 2.1:

  • @Index - An index for the primary key is generated by default in a database. This new annotation will allow to define additional indexes, over a single or multiple columns, for a better performance. This is specified as part of @Table, @SecondaryTable, @CollectionTable, @JoinTable, and @TableGenerator. For example:

    @Table(indexes = {@Index(columnList="NAME"), @Index(columnList="DEPT_ID DESC")})
    @Entity public class Employee {
        . . .
    }

    The generated table will have a default index on the primary key. In addition, two new indexes are defined on the NAME column (default ascending) and the foreign key that maps to the department in descending order.
  • @ForeignKey - It is used to define foreign key constraint or to otherwise override or disable the persistence provider's default foreign key definition. Can be specified as part of JoinColumn(s), MapKeyJoinColumn(s), PrimaryKeyJoinColumn(s). For example:

    @Entity public class Employee {
        @Id private int id;
        private String name;

        @ManyToOne
        @JoinColumn(foreignKey=@ForeignKey(foreignKeyDefinition="FOREIGN KEY (MANAGER_ID) REFERENCES MANAGER"))
        private Manager manager;
        . . .
    }

    In this entity, the employee's manager is mapped by MANAGER_ID column in the MANAGER table. The value of foreignKeyDefinition would be a database specific string.

A complete replay of Linda's talk at JavaOne 2012 can be seen here (click on CON4212_mp4_4212_001 in Media).

These features will be available in GlassFish 4 promoted builds in the near future. The development in EclipseLink is tracked here.

JPA 2.1 will be delivered as part of Java EE 7. The different components in the Java EE 7 platform are tracked here.

JPA 2.1 Expert Group has released Early Draft 2 of the specification. Section 9.4 and 11.2 provide all details about Schema Generation. The latest javadocs can be obtained from here. And the JPA EG would appreciate feedback.

Wednesday Nov 10, 2010

Oredev 2010 Trip Report

Oracle is a silver sponsor sponsor of Oredev 2010 and there were several talks by Oracle speakers. There are about 1300 attendees so its a fairly big conference, probably the biggest developer gathering in the Nordic countries. The conference had a pretty wide variety of topics from Java, .NET, Agile, Smart Phones, Web Development, and others. The conference venue itself used to be a slaughterhouse and you can imagine it easily accommodated 1300 attendees :)

I gave a hands-on workshop on "Deep Dive Hands-on in Java EE 6" and the slides are now available:

There were about 15 participants and most of them were very interactive which is the real fun part! As part of the lab we coded samples for most of the Java EE 6 new/updated technologies such as:

  • Managed Beans 1.0
  • Interceptors 1.1
  • Servlets 3.0
  • Enterprise Java Beans 3.1
  • Contexts & Dependency Injection 1.0
  • Java Server Faces 2.0
  • Java Persistence API 2.0
  • Bean Validation 1.0
  • Java API for RESTful Web services 1.1

The fun part that most of the attendees got their code working along with me. Anyway, the sample code built during the lab is also available here.

I also gave a presentation on "Using the latest Java Persistence API 2.0 Features" and the slide deck is available below:

There were about 50 attendees in this talk. It basically explained the new features in JPA 2 such as improved O/R mapping in Java, expanded JPQL, Metamodel, type-safe Criteria API, support for Bean Validation, standard configuration options, and many other options. And then showed how NetBeans can easily generate JPA entities from a database and also generate canonical metamodel classes. As in the workshop, the audience was very interactive and gave some good suggestions for JPA 2.

Day 1 keynote was by Dr Jeff Norris (JPL, Pasadena) who talked about three pillars of Mission Critical Agility - Vision, Risk, and Commitment. His presentation showed how Alexander Graham Bell had a great vision and risked everything he had to meet that vision. He showed a great demo using augmented reality, probably one of the finest ones I've seen in recent conferences. The keynotes are conducted in a big theater-style hall, that probably makes it fourth conference in Europe (along with Devoxx, Jazoon, and JavaZone) to be conducted in a theater. I wonder if this is a European thing cause I've yet to see a conference in the US going that way ;-)

One of the cool tracks in the conference is Xtra(ck) which has topics like Undesrtanding Hypnosis, Photographic Composition and Creativity, Effective public speaking skills, and some other good ones. I attended the hypnosis session earlier today and Lina Esa (the instructor) talked about unconsious, sub-conscious, and conscious mind and how they can be controlled with hypnosis. She is going to have a practice session on Friday but I'm flying out earlier :(

The closing keynote on Day 1 was by Henrik Kniberg where he talked about how visualizing software, by means of pictures,  diagrams, and otherwise, can drive collaboration and process improvement. He is also linked it very nicely with personal life as a means to release stress and keep the work-life balance.

Other than that there were lots of fun activities planned ...

  • Skinny dipping in the close-to-freezing ocean after sitting in the 85 degrees sauna was quite a unique and cultural experience.
  • Dinner at Ribersborgs Kallabdhus after the hot-and-cold-bath was definitely very welcome.
  • The meet-and-greet at at Magnus's house before the speakers' dinner was very welcoming. Enjoyed Glögg, with traditional raisins and almonds, in this freaking cold weather.
  • The speakers' dinner in a formal setup at Malmo city hall with opening speech by deputy mayor of Malmo was quite an overwhelming experience. I tried black soup (made out of boiled pigs' blood) there and not sure if I really liked it but the ambience was superb ;-)

There are other fun activities planned over the next couple of days. Here are some pics from the event:

And the complete album:

I would've loved to stay longer and engage with the wonderful attendees but fever, cough, and cold is making me pre-pone my flight by a day.

I plan to come back again next year, see ya in 2011!

Technorati: conf oredev sweden glassfish javaee6 jpa2 netbeans 2010

Thursday Oct 28, 2010

TOTD #148: JPA2 Metamodel Classes in NetBeans 7.0 - Writing type-safe Criteria API

NetBeans 7.0 M2 was released recently. There are several Java EE related improvements in this release:

  • Find usages of managed beans (JSF/CDI) and their properties
  • PrimeFaces is now available as an integrated JSF component library
  • Wizard for creating Bean Validation constraint
  • CDI Qualifier creation editor hint
  • Cleaned up Inspect Observer/Producer for CDI events
  • Generation of Bean Validation annotations for Entities

and some others. One of the features that is not much spoken about is the automatic generation of JPA 2 Metamodel classes from Entity classes. This Tip Of The Day (TOTD) will explain how to generate these classes and use them for writing type-safe JPA2 Criteria queries.

The JPA2 Metamodel classes capture the metamodel of the persistent state and relationships of the managed classes of a persistence unit. This abstract persistence schema is then used to author the type-safe queries using Critieria API. The canonical metamodel classes can be generated statically using an annotation processor following the rules defined by the specification. The good thing is that no extra configuration is required to generate these metamodel classes. NetBeans IDE automatically generates the canonical metamodel classes using the EclipseLink Canonical Model Generator. There are two ways these metamodel classes are generated in NetBeans:

  1. Pre-configured when Entity Classes are generated from a Database using the wizards. TOTD #122 provide more details on that. The actual metamodel classes are generated when the project is build using "Clean and Build", "Deploy" or some other related target.
  2. Explicitly configured by right-clicking on the project, "Properties", "Libraries", "Processor", "Add Library...", and select "EclipseLink(JPA 2.0)" and "EclipseLink-ModelGen(JPA 2.0)" libraries and click on "Add Library" as shown below.


This TOTD will use the "Manufacturer" table from the pre-configured "jdbc/sample" JDBC resource in NetBeans and GlassFish. It will create a simple 2-page application where the first page (index.xhtml) accepts a Manufacturer name and the second page (show.xhtml) displays some details about that manufacturer.

  1. Create a NetBeans Web project with the title "CriteriaMetamodel", make sure to enable CDI and Java Server Faces during the creation.
  2. Create "Manufacturer" JPA entity by using the pre-configured "jdbc:derby://localhost:1527/sample" database connection and using the MANUFACTURER table. Notice that the generated manufacturer entity contains the bean validation constraints derived from the database schema, yet another new feature in 7.0 M2. More on this topic in a later blog.
  3. Generate the metamodel classes by right-clicking on the project and selecting "Clean and Build". The generated metamodel class looks like:
    package org.glassfish.samples.entities;
    import javax.annotation.Generated;
    import javax.persistence.metamodel.SingularAttribute;
    import javax.persistence.metamodel.StaticMetamodel;
    @Generated("EclipseLink-2.1.0.v20100614-r7608 @ Mon Oct 25 16:35:03 PDT 2010")
    @StaticMetamodel(Manufacturer.class)
    public class Manufacturer_ { 
        public static volatile SingularAttribute addressline2;
        public static volatile SingularAttribute zip;
        public static volatile SingularAttribute phone;
        public static volatile SingularAttribute addressline1;
        public static volatile SingularAttribute fax;
        public static volatile SingularAttribute manufacturerId;
        public static volatile SingularAttribute email;
        public static volatile SingularAttribute name;
        public static volatile SingularAttribute state;
        public static volatile SingularAttribute city;
        public static volatile SingularAttribute rep;
    }
    

    This is shown as "Generated Sources" in NetBeans IDE as shown:

  4. Generate a new Java class "DatabaseBean" and mark it with "@javax.enterprise.inject.Model" annotation. This class will be the "backing bean" for the JSF pages and will have
    1. A field to accept the manufacturer's name from "index.xhtml"
    2. A field to show information about the searched manufacturer in "show.xhtml"
    3. A business method "searchManufacturer" that searches the database for the given manufacturer's name. This method will use the generated metamodel class and type-safe Criteria API to query the database.

    The complete source code for the class looks like:
    @PersistenceUnit EntityManagerFactory emf;
    
    String name;
    
    Manufacturer manufacturer;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public Manufacturer getManufacturer() {
        return manufacturer;
    }
    
    public void searchManufacturer() {
        EntityManager em = emf.createEntityManager();
    
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery criteria = builder.createQuery(Manufacturer.class);
    
        // FROM clause
        Root root = criteria.from(Manufacturer.class);
    
        // SELECT clause
        criteria.select(root);
    
        // WHERE clause
        Predicate condition = builder.like(root.get(Manufacturer_.name),
                "%" + name + "%");
        criteria.where(condition);
    
        // FIRE query
        TypedQuery query = em.createQuery(criteria);
    
        // PRINT result
        List manufacturers = query.getResultList();
    
        if (manufacturers != null && manufacturers.size() > 0) {
            manufacturer = (Manufacturer)manufacturers.get(0);
        }
    }
    

    The business method returns the first manufacturer whose name contains the text entered in the textbox. No validation is performed in order to keep the business logic simple.

    Notice how "searchManufacturer" method is not using any String-based identifiers for constructing the query graph. This gives the complete type-safety for query construction and allows the errors to be detected much earlier.
  5. Edit the generated "index.xhtml" such that the content within <h:body> looks like:
    <h:form>
     <h:panelGrid columns="3">
     <h:outputText value="Name:" />
     <h:inputText value="#{databaseBean.name}" id="name"/>
     </h:panelGrid>
    
     <h:commandButton
        actionListener="#{databaseBean.searchManufacturer}"
        action="show"
        value="submit"/>
    </h:form>
    

    This page shows a text box and a submit button. The "searchManufacturer" method of the DatabaseBean is invoked when the "submit" button is clicked and passes the entered text in the "name"property of the DatabaseBean.
  6. Create a new XHTML page and name it "show.xhtml". Replace the generated boilerplate code with the code given below:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Show Manufacturer's Detail</title>
      </head>
      <body>
        Name: #{databaseBean.manufacturer.name}<br/>
        Phone: #{databaseBean.manufacturer.phone}<br/>
        Address Line1: #{databaseBean.manufacturer.addressline1}<br/>
        Address Line2: #{databaseBean.manufacturer.addressline2}<br/>
     </body>
    </html>

Now you can deploy the application to GlassFish by usual means and access "http://localhost:8080/CriteriaMetamodel/faces/index.xhtml" which gets displayed as:

Enter some value as "S" in the text box and click on "Submit" to display the result as:

The complete source code for this sample can be downloaded here.

Now Criteria query is little verbose but it does give you the type-safety and was explicitly asked within the JPA Expert Group. It allows you to manipulate different parts of a query such as SELECT, FROM, and WHERE clauses and that too using the Java type system. This reminds me of Mr Potato Head :-)

This behavior can be achieved in JPQL but is available exclusively using String manipulation and the errors are not detected until runtime.


How are you using Criteria API ?

What use cases would you like to see solved in JPA2.next ?

Technorati: totd jpa2 criteria metamodel netbeans javaee6 glassfish

Sunday Aug 16, 2009

TOTD #95: EJB 3.1 + Java Server Faces 2.0 + JPA 2.0 web application - Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3


TOTD #93 showed how to get started with Java EE 6 using NetBeans 6.8 M1 and GlassFish v3 by building a simple Servlet 3.0 + JPA 2.0 web application. TOTD #94 built upon it by using Java Server Faces 2 instead of Servlet 3.0 for displaying the results. However we are still using a POJO for all the database interactions. This works fine if we are only reading values from the database but that's not how a typical web application behaves. The web application would typically perform all CRUD operations. More typically they like to perform one or more CRUD operations within the context of a transaction. And how do you do transactions in the context of a web application ? Java EE 6 comes to your rescue.

The EJB 3.1 specification (another new specification in Java EE 6) allow POJO classes to be annotated with @EJB and bundled within WEB-INF/classes of a WAR file. And so you get all transactional capabilities in your web application very easily.

This Tip Of The Day (TOTD) shows how to enhance the application created in TOTD #94 and use EJB 3.1 instead of the JSF managed bean for performing the business logic. There are two ways to achieve this pattern as described below.

Lets call this TOTD #95.1
  1. The easiest way to back a JSF page with an EJB is to convert the managed bean into an EJB by adding @javax.ejb.Stateless annotation. So change the  "StateList" class from TOTD #94 as shown below:

    @javax.ejb.Stateless
    @ManagedBean
    public class StateList {
        @PersistenceUnit
        EntityManagerFactory emf;

        public List<States> getStates() {
            return emf.createEntityManager().createNamedQuery("States.findAll").getResultList();
        }
    }

    The change is highlighted in bold, and that's it!
Because of "Deploy-on-save" feature in NetBeans and GlassFish v3, the application is autodeployed. Otherwise right-click on the project and select Run (default shortcut "F6"). As earlier, the results can be seen at "http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" or "http://localhost:8080/HelloEclipseLink/faces/template-client.xhtml" and looks like:



The big difference this time is that the business logic is executed by an EJB in a fully transactional manner. Even though the logic in this case is a single read-only operation to the database, but you get the idea :)

Alternatively, you can use the delegate pattern in the managed bean as described below. Lets call this #95.2.
  1. Right-click on the project, select "New", "Session Bean ..." and create a stateless session bean by selecting the options as shown below:



    This creates a stateless session with the name "StateBeanBean" (bug #170392 for redundant "Bean" in the name).
  2. Simplify your managed bean by refactoring all the business logic to the EJB as shown below:

    @Stateless
    public class StateBeanBean {
        @PersistenceUnit
        EntityManagerFactory emf;
        
        public List<States> getStates() {
            return emf.createEntityManager().createNamedQuery("States.findAll").getResultList();
        }
    }

    and

    @ManagedBean
    public class StateList {
        @EJB StateBeanBean bean;

        public List<States> getStates() {
            return bean.getStates();
        }
    }

    In fact the EJB code can be further simplified to:

    @Stateless
    public class StateBeanBean {
        @PersistenceContext
        EntityManager em;
       
        public List<States> getStates() {
            return em.createNamedQuery("States.findAll").getResultList();
        }
    }

    The changes are highlighted in bold.
If the application is already running then Deploy-on-Save would have automatically deployed the entire application. Otherwise right-click on the project and select Run (default shortcut "F6"). Again, the results can be seen at "http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" or "http://localhost:8080/HelloEclipseLink/faces/template-client.xhtml" and are displayed as shown in the screenshot above.

The updated directory structure looks like:



The important point to note is that our EJB is bundled in the WAR file and no additional deployment descriptors were added or existing ones modified to achieve that. Now, that's really clean :)

The next blog in this series will show how managed beans can be replaced with WebBeans, err JCDI.

Also refer to other Java EE 6 blog entries.

Please leave suggestions on other TOTD that you'd like to see. A complete archive of all the tips is available here.

Technorati: totd glassfish v3 mysql javaee6 javaserverfaces jpa2 ejb netbeans

Thursday Aug 13, 2009

TOTD #94: A simple Java Server Faces 2.0 + JPA 2.0 application - Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3


TOTD #93 showed how to get started with Java EE 6 using NetBeans 6.8 M1 and GlassFish v3 by building a simple Servlet 3.0 + JPA 2.0 web application. JPA 2.0 + Eclipselink was used for the database connectivity and Servlet 3.0 was used for displaying the results to the user. The sample demonstrated how the two technologies can be mixed to create a simple web application. But Servlets are meant for server-side processing rather than displaying the results to end user. JavaServer Faces 2 (another new specification in Java EE 6) is designed to fulfill that purpose.

This Tip Of The Day (TOTD) shows how to enhance the application created in TOTD #93 and use JSF 2 for displaying the results.
  1. Right-click on the project, select "Properties", select "Frameworks", click on "Add ..." as shown below:



    Select "JavaServer Faces" and click on "OK". The following configuration screen is shown:



    Click on "OK" to complete the dialog. This generates a whole bunch of files (7 to be accurate) in your project. Most of these files are leftover from previous version of NetBeans and will be cleaned up. For example, "faces-config.xml" is now optional and "forwardToJSF.jsp" is redundant.
  2. Anyway, lets add a POJO class that will be our managed bean. Right-click on "server" package and select "New", "Java Class ...", give the name as "StateList". Change the class such that it looks like:

    package server;

    import java.util.List;
    import javax.faces.bean.ManagedBean;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.PersistenceUnit;
    import states.States;

    /\*\*
     \* @author arungupta
     \*/
    @ManagedBean
    public class StateList {
        @PersistenceUnit
        EntityManagerFactory emf;

        public List<States> getStates() {
            return emf.createEntityManager().createNamedQuery("States.findAll").getResultList();
        }
    }

    Here are the main characterisitcs of this class:
    1. This is a POJO class with @ManagedBean annotation. This annotation makes this class a managed bean that can be used in the JSF pages. As no other annotations or parameters are specified, this is a request-scoped managed bean with the name "stateList" and lazily initialized. More details about this annotation are available in the javadocs.
    2. The persistence unit created in TOTD #93 is injected using @PersistenceUnit annotation.
    3. The POJO has one getter method that queries the database and return the list of all the states.
  3. In the generated file "template-client.xhtml", change the "head" template to:

    Show States

    and "body" template to:

                    <h:dataTable var="state" value="#{stateList.states}" border="1">
                        <h:column><h:outputText value="#{state.abbrev}"/></h:column>
                        <h:column><h:outputText value="#{state.name}"/></h:column>
                    </h:dataTable>

  4. This uses the standard JSF "dataTable", "column", and "outputText" tags and uses the value expression to fetch the values from the managed bean.

If the application is already running from TOTD #93, then Deploy-on-Save would have automatically deployed the entire application. Otherwise right-click on the project and select Run (default shortcut "F6"). The results can be seen at "http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" or "http://localhost:8080/HelloEclipseLink/faces/template-client.xhtml" and looks like:



The updated directory structure looks like:



There were multiple files added by the JSF framework support in NetBeans. But as I said earlier, they will be cleaned up before the final release.

Also refer to other Java EE 6 blog entries.

Please leave suggestions on other TOTD that you'd like to see. A complete archive of all the tips is available here.

Technorati: totd glassfish v3 mysql javaee6 javaserverfaces jpa2 netbeans

Wednesday Aug 12, 2009

TOTD #93: Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3 - A simple Servlet 3.0 + JPA 2.0 app


NetBeans 6.8 M1 introduces support for creating Java EE 6 applications ... cool!

This Tip Of The Day (TOTD) shows how to create a simple web application using JPA 2.0 and Servlet 3.0 and deploy on GlassFish v3 latest promoted build (58 as of this writing). If you can work with the one week older build then NetBeans 6.8 M1 comes pre-bundled with 57. The example below should work fine on that as well.
  1. Create the database, table, and populate some data into it as shown below:

    ~/tools/glassfish/v3/58/glassfishv3/bin >sudo mysql --user root
    Password:
    Welcome to the MySQL monitor.  Commands end with ; or \\g.
    Your MySQL connection id is 1592
    Server version: 5.1.30 MySQL Community Server (GPL)

    Type 'help;' or '\\h' for help. Type '\\c' to clear the buffer.

    mysql> create database states;
    Query OK, 1 row affected (0.02 sec)

    mysql> CREATE USER duke IDENTIFIED by 'glassfish';
    Query OK, 0 rows affected (0.00 sec)

    mysql> GRANT ALL on states.\* TO duke;
    Query OK, 0 rows affected (0.24 sec)

    mysql> use states;
    Database changed

    mysql> CREATE TABLE STATES (
        ->       id INT,
        ->       abbrev VARCHAR(2),
        ->       name VARCHAR(50),
        ->       PRIMARY KEY (id)
        -> );
    Query OK, 0 rows affected (0.16 sec)

    mysql> INSERT INTO STATES VALUES (1, "AL", "Alabama");
    INSERT INTO STATES VALUES (2, "AK", "Alaska");

    . . .

    mysql> INSERT INTO STATES VALUES (49, "WI", "Wisconsin");
    Query OK, 1 row affected (0.00 sec)

    mysql> INSERT INTO STATES VALUES (50, "WY", "Wyoming");
    Query OK, 1 row affected (0.00 sec)

    The complete INSERT statement is available in TOTD #38. Most of this step can be executed from within the IDE as well as explained in TOTD #38.
  2. Download and unzip GlassFish v3 build 58. Copy the latest MySQL Connector/J jar in "domains/domain1/lib" directory of GlassFish and start the application server as:

    ~/tools/glassfish/v3/58/glassfishv3/bin >asadmin start-domain
  3. Create JDBC connection pool and JNDI resource as shown below:

    ~/tools/glassfish/v3/58/glassfishv3/bin >./asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource --restype javax.sql.DataSource --property "User=duke:Password=glassfish:URL=jdbc\\:mysql\\://localhost/states" jdbc/states

    Command create-jdbc-connection-pool executed successfully.
    ~/tools/glassfish/v3/58/glassfishv3/bin >./asadmin ping-connection-pool jdbc/states

    Command ping-connection-pool executed successfully.
    ~/tools/glassfish/v3/58/glassfishv3/bin >./asadmin create-jdbc-resource --connectionpoolid jdbc/states jdbc/jndi_states

    Command create-jdbc-resource executed successfully.

  4. Download NetBeans 6.8 M1 and install "All" version. Expand "Servers" node and add the recently installed GlassFish server.
  5. Create a new Web project and name it "HelloEclipseLink". Make sure to choose "GlassFish v3" as the server and "Java EE 6 Web" as the Java EE version as shown below:



    Take defaults elsewhere.
  6. Create the Persistence Unit
    1. Right-click on the newly created project and select "New", "Entity Classes from Database ...". Choose the earlier created data source "jdbc/jndi_states" as shown below:

    2. Select "STATES" table in "Available Tables:" and click on "Add >" and then "Next >".
    3. Click on "Create Persistence Unit ...", take all the defaults and click on "Create". "EclipseLink" is the Reference Implementation for JPA 2.0 is the default choosen Persistence Provider as shown below:

    4. Enter the package name as "server" and click on "Finish".
  7. Create a Servlet to retrieve and display all the information from the database
    1. Right click on the project, "New", "Servlet ...".
    2. Give the Servlet name "ShowStates" and package "server".
    3. Even though you can take all the defaults and click on "Finish" but instead click on "Next >" and the following screen is shown:



      Notice "Add information to deployment descriptor (web.xml)" checkbox. Servlet 3.0 makes "web.xml" optional in most of the common cases by providing corresponding annotations and NetBeans 6.8 leverages that functionality. As a result, no "web.xml" will be bundled in our WAR file. Click on "Finish" now.

      The generated servlet code looks like:



      Notice @WebServlet annotation, this makes "web.xml" optional. TOTD #82 provide another example on how to use Servlet 3.0 with EJB 3.1.
    4. Inject the Persistence Unit as:

          @PersistenceUnit
          EntityManagerFactory emf;

      right above "processRequest" method.
    5. Change the "try" block of "processRequest" method to:

                  List<States> list = emf.createEntityManager().createNamedQuery("States.findAll").getResultList();
                  out.println("<table border=\\"1\\">");
                  for (States state : list) {
                      out.println("<tr><td>" + state.getAbbrev() +
                              "</td><td>" + state.getName() +
                              "</td></tr>");
                  }
                  out.println("</table>");

      This uses a predefined query to retrieve all rows from the table and then display them in a simple formatted HTML table.
  8. Run the project
    1. Right click on the project, select "Properties" and change the "Relative URL" to "/ShowStates". This is the exact URL that you specified earlier.

    2. Right-click on the project and select "Run" to see the following output:



So we created a simple web application that uses Servlet 3.0, JPA 2.0, EclipseLink and deployed on GlassFish v3 using NetBeans 6.8 M1. NetBeans provides reasonable defaults making you a lazy programmer. Believe this is more evident when you start playing with Java EE support in other IDEs ;-)

Finally, lets look at the structure of the generated WAR file:



It's very clean - no "web.xml", only the relevant classes and "persistence.xml".

Also refer to other Java EE 6 blog entries. A future blog entry will show how to use JSF 2.0 instead of Servlet for displaying the results.

Please leave suggestions on other TOTD that you'd like to see. A complete archive of all the tips is available here.

Technorati: totd glassfish v3 mysql javaee6 servlet3 jpa2 netbeans
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today