0

In Java EE, when using JPA to persist classes, my persistance.xml file looks like this:

<?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="JMonks_Crud">
        <jta-data-source>jdbc/example</jta-data-source>
        <class>models.Class1</class>
        <class>models.Class2</class>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="both"/>
        </properties>
    </persistence-unit>
</persistence>

The only experience so far i have with JPA is from an example that is supposed to clear all the values in the database, then there is servlet's post method to hardcode some values into the used. To accomplish this, the example uses:

<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>

However I want to persist the data in the database between sessions (like any good dynamic site), and i do not know what to put in the value attribute to do accomplish this. the wikia site (http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#Using_EclipseLink_JPA_Extensions_for_Schema_Generation) says that these are the three different values that it can place in this value:

  • none – EclipseLink does not generate DDL; no schema is generated.
  • create-tables – EclipseLink will attempt to execute a CREATE TABLE SQL for each table. If the table already exists, EclipseLink will follow the default behavior of your specific database and JDBC driver combination (when a CREATE TABLE SQL is issued for an already existing table). In most cases an exception is thrown and the table is not created. EclipseLink will then continue with the next statement. (See also eclipselink.create-ddl-jdbc-file-name.)
  • drop-and-create-tables – EclipseLink will attempt to DROP all tables, then CREATE all tables. If any issues are encountered, EclipseLink will follow the default behavior of your specific database and JDBC driver combination, then continue with the next statement. (See also eclipselink.create-ddl-jdbc-file-name and eclipselink.drop-ddl-jdbc-file-name.)

if I use the none attribute, will it grab the existing tables with populated data? or will none make no tables at all and not help? if i use create-tables, won't i just get an exception thrown? as the table I am trying to use already exists.

thanks for your time :)

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Josh Monks
  • 129
  • 1
  • 2
  • 9
  • After about an hour of fiddling, the problem seems to have solved itself, the attribute is still set to "drop-and-create-tables", but the data is now saved. – Josh Monks May 27 '12 at 02:31

1 Answers1

0

If you use create-tables, EclipseLink will create the tables as defined by your entities when deployment occurs. If you use drop-and-create-tables, you may run into problems as it will first attempt to drop the tables for each entity, and then recreate them. This might cause you to lose data if you redeploy the app or restart the application. This is mostly for development and testing when the tests populate the database each time it starts.

If using EclipseLink 2.4 (a nightly build) you can also use create-or-extend-tables which will use alter table statements on existing tables if you add fields to your entities on redeployment.

Chris
  • 20,138
  • 2
  • 29
  • 43