...
An example of working persistence.xml for OpenJPA:
Code Block |
---|
| xml |
---|
| xml |
---|
title | persistence.xml | xml |
---|
|
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="movie-unit">
<jta-data-source>movieDatabase</jta-data-source>
<non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
<class>org.superbiz.injection.jpa.Movie</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
</properties>
</persistence-unit>
</persistence>
|
Where the datasources above are configured in your openejb.xml as follows:
Code Block |
---|
| xml |
---|
| xml |
---|
title | openejb.xml | xml |
---|
|
<Resource id="movieDatabase" type="DataSource">
JdbcDriver = org.hsqldb.jdbcDriver
JdbcUrl = jdbc:hsqldb:mem:moviedb
</Resource>
<Resource id="movieDatabaseUnmanaged" type="DataSource">
JdbcDriver = org.hsqldb.jdbcDriver
JdbcUrl = jdbc:hsqldb:mem:moviedb
JtaManaged = false
</Resource>
|
Or in properties as follows:
Code Block |
---|
| xml |
---|
| xml |
---|
title | java.lang.System or InitialContext propertiesxml |
---|
|
p.put("movieDatabase", "new://Resource?type=DataSource");
p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
p.put("movieDatabaseUnmanaged", "new://Resource?type=DataSource");
p.put("movieDatabaseUnmanaged.JdbcDriver", "org.hsqldb.jdbcDriver");
p.put("movieDatabaseUnmanaged.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
p.put("movieDatabaseUnmanaged.JtaManaged", "false");
|
...
Someone has to create the database structure for your persistent objects. If you add the openjpa.jdbc.SynchronizeMappings property as shown below OpenJPA will auto-create all your tables, all your primary keys and all foreign keys exactly to match your objects.
Code Block |
---|
| xml |
---|
| xml |
---|
title | persistence.xmlxml |
---|
|
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="movie-unit">
<!-- your other stuff -->
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
</properties>
</persistence-unit>
</persistence>
|
...
on a persistence unit setup as RESOURCE_LOCAL ...
Code Block |
---|
| xml |
---|
| xml |
---|
title | persistence.xmlxml |
---|
|
<persistence-unit name="movie-unit" transaction-type="RESOURCE_LOCAL">
...
</persistence-unit>
|
...