Using Java2DB feature of GlassFish v2
GlassFish V2 provides Java2DB feature which let users to specify database schema information in a JPA application. The persistence provider will use the information to define the corresponding schema in a database. I am using a Datatype test application as an example below to show how to use the Java2DB feature of GlassFish V2.
Install GlassFish V2 and Create JDBC connection
-
Download GlassFish V2 and install at [GF_Install_Dir].
-
Include [GF_Install_Dir]/bin in PATH for convenience.
-
Start GlassFish V2 domain and Derby DB with
~/testbed > asadmin start-domain
~/testbed > asadmin start-database
|
-
Create JDBC connection-pool:
~/testbed >
asadmin create-jdbc-connection-pool --user admin --terse=false --target server
--property User=dbuser:Password=dbpassword:dataBaseName=testdb:serverName=localhost:portNumber=1527:create=true --echo=true --port 4848 --allownoncomponentcallers=false --interactive=true --passwordfile ~/testbed/adminpassword.txt --host localhost --datasourceclassname org.apache.derby.jdbc.ClientDataSource --nontransactionalconnections=false --restype javax.sql.DataSource datatype-pool1
|
-
Create JDBC resource:
~/testbed > asadmin create-jdbc-resource --interactive=true --passwordfile ~/testbed/adminpassword.txt --user admin --terse=false --enabled=true --host localhost --target server --echo=true --connectionpoolid datatype-pool1 --port 4848 jdbc/datatype1
|
Create the Datatype Test Application to Use Java2DB
Download
the Datatype test and unzip into ~/testbed directory. Or use the following steps to create your own Datatype test application:
-
Create the Datatypes entity beans and session bean.
-
Create the persistence.xml and set property "toplink.ddl-generation" to "drop-and-create-tables" to use the Java2DB feature:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
<persistence-unit name ="manager">
<jta-data-source>jdbc/datatype1</jta-data-source>
<properties>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
-
Package the Datatype Test Application into an EAR file. The file/directory structure of ejb-cmp-datatypesApp.ear in the zip file above can be used as an example.
Deploy the Datatype Test Application and Run
-
Deploy the Datatype Test App to GlassFish V2 and the schema will be created during deployment:
~/testbed > asadmin deploy --user admin --terse=false --target server --echo=true --upload=true --port 4848 --interactive=true --passwordfile ~/testbed/adminpassword.txt --availabilityenabled=false --enabled=true --retrieve ~/testbed/ejb-cmp/archive --host localhost --generatermistubs=false --precompilejsp=false --force=true --name ejb-cmp-datatypesApp --verify=false ~/testbed/ejb-cmp/archive/ejb-cmp-datatypesApp.ear
|
-
Run the application with appclient:
~/testbed > appclient -client ~/testbed/ejb-cmp/archive/ejb-cmp-datatypesAppClient.jar EJB3_DataTypes_Java2DB
|
The following output from the Datatype test will be printed on console:
-----------------------------------------
- Update_row : PASS -
- Query_Integer : PASS -
- Read_row: PASS -
- Populate_INTEGER_MAXVAL: PASS -
- Query_Long : PASS -
- : PASS -
- Create_row_ZEROVAL: PASS -
- Average_LongData : PASS -
- Average_FloatData : PASS -
- Query_Short : PASS -
- Query_Byte : PASS -
- Query_SqlDate : PASS -
- Average_ByteData : PASS -
- Populate_LONG_MAXVAL: PASS -
- Create_row_MEDIANVAL : PASS -
- Average_IntegerData : PASS -
- Query_Float : PASS -
- Populate_BYTE_MAXVAL: PASS -
- Populate_SHORT_MAXVAL: PASS -
- Average_ShortData : PASS -
- Query_Row_MAX_Value : PASS -
- Query_UtilDate : PASS -
- Query_TimeStamp : PASS -
- Query_Character : PASS -
- Delete_row : PASS -
-----------------------------------------
Total PASS: 25
Total FAIL: 0
Total DNR: 0
-----------------------------------------
Posted at
01:16PM Jun 10, 2008
by Ming Zhang in Sun |