I was attempting to create a database for a new project which just contained entities – I had imported the entity classes from Enterprise Architect and added the required annotations, getters & setters etc.
My first attempt used the following persistence.xml (I have removed most of the classes for the example)
<?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=”Test” transaction-type=”RESOURCE_LOCAL”>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>jdbc/TestPool</non-jta-data-source>
<class>uk.co.salientsoft.Test.domain.Address</class>
<class>uk.co.salientsoft.Test.domain.AddressLine</class>
<class>uk.co.salientsoft.Test.domain.Campaign</class>
<class>uk.co.salientsoft.Test.domain.CampaignText</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name=”eclipselink.logging.level” value=”INFO” />
<property name=”eclipselink.target-server” value=”None” />
<!– <property name=”eclipselink.target-server” value=”SunAS9″ />–>
<property name=”eclipselink.target-database” value=”Oracle” />
<property name=”eclipselink.ddl-generation.output-mode” value=”database” />
</properties>
</persistence-unit>
</persistence>
Having created the database and validated the connection/datasource etc., I attempted to create the tables from Eclipse by right clicking the project and selecting JPA Tools/Create Tables from Entities…
This resulted in the following error:-
[EL Config]: Connection(5226838)–connecting(DatabaseLogin(
platform=>OraclePlatform
user name=> “Test”
connector=>JNDIConnector datasource name=>jdbc/TestPool
))
[EL Severe]: Local Exception Stack:
Exception [EclipseLink-7060] (Eclipse Persistence Services – 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/TestPool].
Internal Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
The error occurred even though the data source was being used outside the container as a non-jta data source (this worked previously for JPA1/Glassfish V2, allowing JPA to be used outside the container but still using a datasource defined in Glassfish). A previous post here (in the section Notes on Persistence.xml) also reports that when running outside the container, it is vital to have eclipselink.target-server defaulted or set to none. In this case, the setting caused no difference either way. After a fair bit of research/Googling, I could not find the cause of the problem, so I simply reverted to specifying the connection explicitly in persistence.xml, without using the connection defined in Glassfish at all.
This worked correctly. I will not need to create the database very often, and it only involved tweaking a few lines in the file, so I’m happy to live with it. Here is my modified persistence.xml which uses a direct connection specified in the file. This version created the database correctly. Note that as detailed in this post here (near the bottom of the post), the property names for JDBC url, user, password and driver have now been standardised and are no longer eclipslink specific.
<?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=”Test” transaction-type=”RESOURCE_LOCAL”>
<!– <persistence-unit name=”Test” transaction-type=”JTA”>–>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!–<jta-data-source>jdbc/TestPool</jta-data-source>–>
<!–<non-jta-data-source>jdbc/TestPool</non-jta-data-source>–>
<class>uk.co.salientsoft.Test.domain.Address</class>
<class>uk.co.salientsoft.Test.domain.AddressLine</class>
<class>uk.co.salientsoft.Test.domain.Campaign</class>
<class>uk.co.salientsoft.Test.domain.CampaignText</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name=”javax.persistence.jdbc.url” value=”jdbc:oracle:thin:@localhost:1521:xe”/>
<property name=”javax.persistence.jdbc.user” value=”Test”/>
<property name=”javax.persistence.jdbc.password” value=”Test”/>
<property name=”javax.persistence.jdbc.driver” value=”oracle.jdbc.OracleDriver”/>
<property name=”eclipselink.logging.level” value=”INFO” />
<property name=”eclipselink.target-server” value=”None” />
<!– <property name=”eclipselink.target-server” value=”SunAS9″ />–>
<property name=”eclipselink.target-database” value=”Oracle” />
<property name=”eclipselink.ddl-generation.output-mode” value=”database” />
</properties>
</persistence-unit>
</persistence>