0
votes

I am new to Java EE and JPA world, and need help with understanding persistence.xml file content. I work with Eclipse IDE, TomEE application server, OpenJPA as persistence provider, and MySQL as database.

Now, lets say I have some Java EE project with annotated entities named A,B and C, and I would like to map these entities to MySQL database named TestDB. I would like for database to be created during deployment, according to entity annotations. On MySQL server I log as "root" user with password "123".

Which properties from persistence.xml correspond to which Java EE environment artifacts (what represents persistence unit, where to specify database name ...)? Is this the correct persistence.xml file content, for situation described above:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">

    <persistence-unit name="Test" transaction-type="JTA">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <properties>

            <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/TestDB" />
            <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
            <property name="openjpa.ConnectionUserName" value="root" />
            <property name="openjpa.ConnectionPassword" value="123" />
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
            <property name="openjpa.jdbc.DBDictionary" value="mysql" />
            <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
        </properties>
    </persistence-unit>

</persistence>

What else settings are important for JPA to work properly? (Appreciate any help)

1
This question is a little bit to broad: Which problems did you have when you tried that persistence.xml? - Beryllium
I wrote code above following some documentation on setting persistence.xml, but it doesn't mean it is correct. I am not sure about couple of things: What represents persistence-unit? How to set persistence.xml properties so that database schema is generated? - Vladimir
Actually, I have an JEE example (that should be valid), but can't make it run. This is the part of log message I am getting when try to run ant build file:[code] [java] WARNING: RequestFailed{server=127.0.0.1:8080/tomee/ejb} JNDI_LOOKUPnull:/InitBeanRemote {error=Cannot open input stream to server: } [java] Exception in thread "main" org.apache.openejb.client.ClientRuntimeException: Invalid response from server: -1[/code] - Vladimir

1 Answers

0
votes

Above persistence.xml is missing datasource tag: <jta-data-source>TestDB-jta-DS</jta-data-source>

Then, for Apache TomEE, data source can be defined in [tomee]/conf/tomee.xml configuration file. So, proper datasource definition for above persistence.xml would be:

<Resource id="TestDB-jta-DS" type="DataSource">
    JdbcDriver          com.mysql.jdbc.Driver
    JdbcUrl             jdbc:mysql://127.0.0.1:3306/TestDB
    UserName            root
    Password            123
    JtaManaged          true
    DefaultAutoCommit   false
</Resource>