newfolder
newfolder

Reputation: 108

How to create tables from entity classes in JPA on server Startup?

I am supposed to check for existence of tables on server startup. If they did not exist I have to create them using entity classes. Is this even possible? I am using Eclipse and my server is wildfly10. I am connecting to Oracle 11g xe but I don't think it is database that is causing issues

Anyway, this is what I have done so far

My persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"                 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="Lab5">
        <properties>
            <property name="javax.persistence.schema-generation.database.action" value="create"/>
        </properties>
    </persistence-unit>
</persistence>

Without above property my database query throws Exception. But what is really strange, is that with this property query returns empty list, but table does not exist in database. Also, I noticed warning showing on server startup

HHH000431: Unable to determine H2 database version, certain features may not work

I tried various fixes for this warning found on stackoverflow and other sites but they did not help. I don't even know if this is related to my problem

Upvotes: 1

Views: 3764

Answers (2)

Chris
Chris

Reputation: 21165

JPA 2.1 made DDL generation a part of the specification. You can use the "javax.persistence.schema-generation.database.action" persistence property with a "create" value to have 2.1 providers create the database schema for you during deployment. JPA can be used to generate or even run custom scripts should you need to modify the table creation/tear down and population process.

The problem you are likely encountering is your persistence unit does not specify a datasource, leaving it up to the container to figure out which you want to connect to, and Wildfly must be defaulting to an H2 database. The tables will be setup in this database, but not the Oracle XE database you are expecting - which is why queries return no values and yet in your console, the tables don't even exist.

You need to setup a datasource to your database in the server, and then point your persistence unit to it using the <non-jta-data-source> or <jta-data-source>. Your persistence.xml is incomplete, so I would urge you to look at a demo persistence unit first.

Upvotes: 3

Chris
Chris

Reputation: 68

This will likely depend on the implementation you're including with your project. Assuming you're using hibernate for persistence, you'll need to add the following:

...
<persistence-unit ...>
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  ...
  <properties>
    <property name = "hibernate.hbm2ddl.auto" value="create" />
    ...
  </properties>
</persistence-unit>

Note that this will assume that your connection is formed with a user having the required privileges in the db for table creation. Take a look at the hibernate docs here.

Upvotes: 2

Related Questions