glassfish/modules

unzip -p org.eclipse.persistence.core.jar META-INF/MANIFEST.MF | grep Bundle-Version

Bundle-Version: 2.5.0.v20130110-d839ca4

@Entity

@Table

@NamedQueries({

@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")

})

public class Employee implements Serializable {

private static final long serialVersionUID = 1L;

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private int id;



@Column(length=40)

private String name;



Employee

id

name

@NamedQuery

persistence.xml

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

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="MyPU" transaction-type="JTA">

<jta-data-source>jdbc/sample</jta-data-source>

<properties>

<property name="javax.persistence.schema-generation-action" value="drop-and-create"/>

<property name="javax.persistence.schema-generation-target" value="database"/>

<property name="eclipselink.deploy-on-startup" value="true"/>

</properties>

</persistence-unit>

</persistence>





<persistence-unit>

<jta-data-source>

javax.persistence.schema-generation-action

javax.persistence.schema-generation-target

jdbc/sample

@Stateless

public class EmployeeBean {



@PersistenceContext

EntityManager em;



public void persist(Employee e) {

em.persist(e);

}



public List<Employee> get() {

return em.createNamedQuery("Employee.findAll", Employee.class).getResultList();

}

}





for (int i=0; i<5; i++) {

bean.persist(new Employee("Name" + i));

}

for (Employee e : bean.get()) {

out.println(e.getName() + "<br>");

}



<properties>

<property name="javax.persistence.schema-generation-action" value="drop-and-create"/>

<property name="javax.persistence.schema-generation-target" value="scripts"/>

<property name="javax.persistence.ddl-create-script-target" value="createfoo.sql"/>

<property name="javax.persistence.ddl-drop-script-target" value="dropfoo.sql"/>

<property name="eclipselink.deploy-on-startup" value="true"/>

<property name="eclipselink.application-location" value="/tmp"/>

</properties>



action

target

*.script-target

more /tmp/createfoo.sql

CREATE TABLE EMPLOYEE (ID INTEGER NOT NULL, NAME VARCHAR(40), PRIMARY KEY (ID))

CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(15), PRIMARY KEY (SEQ_NAME))

INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 0)

CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(15), PRIMARY KEY (SEQ_NAME))

INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 0)



more /tmp/dropfoo.sql

DROP TABLE EMPLOYEE

DELETE FROM SEQUENCE WHERE SEQ_NAME = 'SEQ_GEN'



glassfish/domains/domain1

As href="https://blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation">TOTD#187 explained, one of the new features in JPA 2.1 is SchemaGeneration. It refers to generation of database artifacts liketables, indexes, and constraints in a database schema. Read href="https://blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation">TOTD#187 for more details. Thisipheay (TOTD) will explain how to use this feature in GlassFish4.JPA 2.1 is implemented in EclipseLink and the href="http://wiki.eclipse.org/EclipseLink/Development/JPA_2.1">statusshows that a decent progress is made. href="http://www.eclipse.org/eclipselink/downloads/milestones.php">EclipseLinkMilestone builds shows the dates when milestones are released.It typically takes a few days for the milestone to be integrated inGlassFish 4 after the release.How do you know which milestone is integrated in GlassFish 4 ?Issue the following commmand indirectory:to see an output as:If we break the version string then it shows this is version 2.5.0and dated 20130110 in the format YYYYMMDD, that would be Jan 10,2013. Based upon the href="http://www.eclipse.org/eclipselink/downloads/milestones.php">milestonerelease dates, this indicates M6 is integrated.I tried this with the href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/nightly/latest-glassfish.zip">latestnightly build (dated Jan 18). By the time you read this blog,a newer version may be integrated and so the version string may lookdifferent.Now lets see this in action!The sample code explained below can be href="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/File/ad1e5b049d13eee2139d541c9478d0be/totd195_schema_gen.zip">downloadedhere and run on href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/nightly/glassfish-4.0-b72-01_17_2013.zip">GlassFish4 1/17 nightly. A promoted build after this nightly shouldwork too.Create an Entity class as:This is a simpleentity that has two fieldsand. A trivialisdefined to retrieve the list of employees.The associatedlooks like:Other than the usual elements likeand, the newly introducedproperties -anddefinethe action and target for the schema generation. The action is todrop and create the tables. The target defines to perform the actionin the database. These properties control the behavior of schemageneration and their meaning is defined in href="https://blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation">TOTD#187. Notice, the third property is EclipseLink specific andis specified to eagerly initialize the Persistence Unit. This willensure that the schema is generated during the applicationdeployment itself. Otherwise it is generated when the PU is firstaccessed.This will generate the table in the database defined byJDBC resource. This resource is pre-defined for JavaDB that isalready bundled with GlassFish 4.After this, you can write a simple EJB as:And invoke it from a Servlet as:A different set of properties may be specified as:These properties specify theas"drop-and-create", i.e. drop and create the tables. Thespecifies the the action to be performed in a script. Theproperty specifies the name of the files for create and drop DDL.The following scripts are generated:By default, the scripts are generated indirectory. In this case, an EclipseLink-specific property toidentify the base location of the generated files is specified.As href="https://blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation">TOTD#187 explains, you can bundle DDL and DML scripts to generateand populate the database. Are you ready to test drive href="http://jcp.org/en/jsr/detail?id=338">JPA 2.1 implementedusing href="http://wiki.eclipse.org/EclipseLink/Development/2.5.0">EclipseLink2.5 in href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/">GlassFish 4 href="http://download.oracle.com/otndocs/jcp/persistence-2_1-pr-spec/index.html">JPA2.1 Public Review specification has been available for a fewdays now. The Appendix A in the specification provide revisionhistory in case you want to focus on the newly added featuresonly. If you are a JUG member, you may consider href="http://glassfish.java.net/adoptajsr/">adopting this JSRand provide

feedback