Question

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)

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top