Glassfish and Automatic Table Generation (Java2db)

All | Personal | Sun
Main | Pramod Gopinath's... »
20051207 Wednesday December 07, 2005

Pramod Gopinath's Weblog

Glassfish and Automatic Table Generation


Glassfish has a feature by which we could automatically create the database schema at the time of application deployment. This is also referred to as the “java2db” feature. Having this feature enables the developer to focus on creating the entity beans and let the application server define tables based on the fields in the entity beans and the relationship between them.

Currently we support java2db from within the application server environment. Very shortly we would be adding support for this feature in the out of container case.


How to enable the java2db feature for a ejb 3.0 ear ?

To enable java2db for an ejb 3.0 ear define the following properties for a persistence unit descriptor in the persistence.xml.

Property Name

Description

toplink.ddl-generation

Possible values of “create-tables"/"drop-and-create-tables"/"none". The default value is “none”.

toplink.create-ddl-jdbc-file-name

Name of the create jdbc ddl file

toplink.drop-ddl-jdbc-file-name

Name of the drop jdbc ddl file

toplink.application-location

In the context of the application server, this property is not to be defined by the user. In the code we set this property to be the app generated directory.

This property would be used for the out of container case to define the location where one would want the jdbc ddl files to be stored.


e.g. of a persistence.xml usage could be


Example of a simple persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name ="em1">
        <jta-data-source>jdbc/DataSource1</jta-data-source>
        <properties>
          <property name="toplink.ddl-generation" value="drop-and-create-tables"/>
          <property name="toplink.create-ddl-jdbc-file-name" value="create_ora.jdbc"/>
          <property name="toplink.drop-ddl-jdbc-file-name" value="drop_ora.jdbc"/>
        </properties>
    </persistence-unit>
</persistence>


How to enable java2db feature w/o changing the ejb3.0 ear ?

The easiest way to enable this feature w/o changing the application ear is by using the asadmin cli commands :

These are the same commands that work with our application server for cmp2.x beans. These options overwrite the setting of the properties that might be defined in the persistence.xml.

example :

  1. Command to deploy an ejb3.0 application that does not contain the properties defined but still create the required tables in the database :

    $GLASSFISH_HOME/bin/asadmin deploy --retrieve . --user admin --password adminadmin --host localhost --port 4848 --createtables=true --name onetomany --force=true /export/home/tools/ejb30_related/persistence-onetomanyApp.ear

  2. Command to redeploy an ejb3.0 application that does not contain the properties and ensure that the tables get recreated in the database :

    $GLASSFISH_HOME/bin/asadmin deploy --retrieve . --user admin --password adminadmin --host localhost --port 4848 --dropandcreatetables=true --name onetomany --force=true /export/home/tools/ejb30_related/persistence-onetomanyApp.ear

  3. Command to undeploy the ejb3.0 application that does not contain the properties and ensure that the tables get dropped from the database :

    $GLASSFISH_HOME/bin/asadmin undeploy --user admin --password adminadmin --host localhost --port 4848 --droptables=true onetomany


How does the admin cli commands interact/affect the properties if they are defined in persistence.xml ?

The cli options (--createtables/--dropandcreatetables/--droptables) take precedence over the properties in the persistence.xml.

Lets take an example to further explain this :

The user has defined the following in their persistence.xml -> toplink.ddl-generation : drop-and-create-tables

and then when deploying the application has specified -> --createtables=false.

In this case we would create the jdbc ddl statements but we will not execute the ddl statements against the database.

But if the cli option --createtables option is not defined at the deploy time, we would create the jdbc ddl statements and also execute the ddls against the database.


What happens if the jdbc ddl file names are not specified in the persistence.xml ?

This situation is similar to the cmp2.x case and we end up create ddl files with some default names. We try to create the name of the file by prepending the application and/or module name followed by the persistence unit name and then a static string of the form "createDDL.jdbc" or "dropDDL.jdbc".

So you might see files with names like

default_em_dropDDL.jdbc and default_em_createDDL.jdbc

OR

realApp_war1_ejb_em1_dropDDL.jdbc and realApp_war1_ejb_em1_createDDL.jdbc

(this is case where the persistence.xml is defined in a war file)


How to enable java2db for Derby database?

To enable java2db feature for the bundled Derby database ensure that the persistence unit section of your persistence.xml file has the following entries:

    <persistence-unit name =...>
        <!-- the datasource pointing to the bundled derby database -->
        <jta-data-source>jdbc/__default</jta-data-source>
        <properties>
          <!--Enable the java2db feature -->
          <property name="toplink.ddl-generation" value="drop-and-create-tables"/>
        </properties>
    </persistence-unit>
</persistence>

Do all the asadmin command options for java2db defined for Sun Application Server 8.x work as is with Glassfish ?

Not at this point of time. There are options like –uniquetablenames and –dbvendorname that can be defined for Sun Application Server 8.x and are currently not supported in Glassfish. If and when we introduce support for these features this document would be updated to reflect the same.


Which are the source files in Glassfish that deal with providing support for the various databases ?

Below is chart that provides information about the internal source files and the corresponding databases that they support in Glassfish.

Database Name

Platform

Oracle
oracle.toplink.essentials.platform.database.oracle.OraclePlatform

Cloudscape

oracle.toplink.essentials.platform.database.CloudscapePlatform

Derby

oracle.toplink.essentials.platform.database.DerbyPlatform

DB2

oracle.toplink.essentials.platform.database.DB2Platform

Sybase

oracle.toplink.essentials.platform.database.SybasePlatform

SqlServer

oracle.toplink.essentials.platform.database.SQLServerPlatform

Informix

oracle.toplink.essentials.platform.database.InformixPlatform

PointBase

oracle.toplink.essentials.platform.database.PointBasePlatform

Mysql

oracle.toplink.essentials.platform.database.MySQL4Platform

PostgreSQL
oracle.toplink.essentials.platform.database.PostgreSQLPlatform


Posted by pramodg ( Dec 07 2005, 12:11:06 PM PST ) Permalink Comments [12]

Trackback URL: http://blogs.sun.com/java2dbInGlassFish/entry/automatic_table_generation_feature_in
Comments:

Nice article.

1) What happens if I use dropandcreate while deploying for the first time and there are no tables in the database? Does it simply ignore the drop part and proceed to create tables? It would be nice to behave this way because if user is using properties in persistence.xml, then they have to remember to set it to createtables for the first time and change it to dropandcreate unless they use droptables=true during undeployment.

2) Similarly what happens when user specifies createtables and tables already exist?

Look forward to not requiring DatabasePlatform. This seem to be following Hibernate path.

-- Sahoo

Posted by Sahoo on December 07, 2005 at 06:18 PM PST #

If there are no tables to drop, the drop part will be ignored. If the tables already exist, the deployment won't fail, but 'asadmin deploy' will display the warnings. Your application might still run, if the existing tables match the expected DDL. Also note that –uniquetablenames and –dbvendorname still work for CMP beans. -marina

Posted by Marina on December 08, 2005 at 12:36 AM PST #

Any plans to support other databases anytime soon? Postgresql/Firebird come to mind. Also what about an update feature where columns are added if they don't exist without dropping the data? [Hibernate has this]

Posted by Eric on December 08, 2005 at 06:55 AM PST #

Agree with previous poster, would really like to see Postgres supported.

Posted by Nils on December 09, 2005 at 02:30 AM PST #

Why is the list missing actual Oracle integration?

Posted by Mikael Gueck on December 16, 2005 at 12:23 AM PST #

Seems like "oracle.toplink.essentials.platform.database.DerbyPlatform" is working with postgreSQL 8.0.

Posted by aung zay on December 16, 2005 at 06:05 AM PST #

Oracle is the default platform hence you do not have to define a platform for Oracle. The platforms defined in the blog are meant for all databases other than Oracle.

Posted by Pramod Gopinath on December 19, 2005 at 12:54 PM PST #

We have update Glassfish to automatically detect the database platform. So for the databases that are listed in the blog you do not need to specify the database platform. For further details refer to http://blogs.sun.com/roller/page/GlassFishPersistence?entry=database_platform_autodetection

Posted by 192.18.43.11 on February 02, 2006 at 04:00 PM PST #

How much sense does it make, to specify a datasource in one place and the properties it might [not] have in a totally different place ?

Posted by jel on February 20, 2006 at 10:40 PM PST #

Hello: I need the follow library: oracle.toplink.essentials.platform.database.PostgreSQLPlatform where can I found it? thank you .

Posted by Marcelo Ceresola on September 08, 2006 at 01:28 PM PDT #

Automatic timetable generator software is available at http://www.automatictimetable.com.

Posted by rajeev katula on September 23, 2006 at 05:07 AM PDT #

Hi Pramod,

Is it possible somehow that while using toplinks, we can somehow manipulate the order of the columns (for the table to be created) to match the order in which the fields are defined in the entity class with java persistence api?

Posted by Sunny on February 01, 2008 at 04:23 AM PST #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed

Calendar

RSS Feeds

Search

Links

Navigation

Referers