Java Reference
In-Depth Information
We have highlighted the most important attributes in persistence.xml . The name
attribute is a mandatory element, which will be used to reference the persistence unit from
our Enterprise JavaBeans.
In the example code, we use the WildFly built-in memory H2 database ( ht-
tp://www.h2database.com/ ) available by default at java:jboss/datasources/
ExampleDS (so that it is possible to run the example without any setup). However, you
can use a configured PostgreSQL connection here, java:jboss/datasources/
wflydevelopment , which we created earlier. In Java EE 7, you could even omit the
whole jta-data-source tag. Every container is now obliged to provide a default data
source for applications to use. For WildFly, it would be the aforementioned H2 database.
We also define the classes that should be considered as entities. This is an optional step; if
the entities are in the same archive as the persistence.xml file, they will be autodis-
covered.
In previous JPA versions, almost every configuration needed some provider-specific prop-
erties. In JPA 2.1, a number of standard properties were added, such as presented
javax.persistence.schema-generation.database.action . The drop-
and-create value can be used to create and drop your database tables each time you
deploy your application. This can be an advantage if you want to start with a clean storage
each time you deploy the application.
However, it is also possible to instruct JPA to generate SQL scripts for you, so you can
manually apply them to the database. Simply add the following entries to your
persistence-unit tag:
<property name="javax.persistence.schema-generation-target"
value="scripts"/>
<property name="javax.persistence.ddl-create-script-target"
value="createSeats.sql"/>
<property name="javax.persistence.ddl-drop-script-target"
value="dropSeats.sql"/>
If you don't specify the location by specifying an additional property, then the generated
scripts will be placed in the JBOSS_HOME/bin directory, with the names that you
provided in the configuration. The names can be absolute paths, so you can get the scripts
to any place in your filesystem (if WildFly is permitted to write them there of course).
Search WWH ::




Custom Search