You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by Kay Wrobel <ka...@gmx.net> on 2015/04/24 17:47:25 UTC

Initialize Database Via Persistence.xml

Hi everybody.

I am having a rough time finding a way to initialize an Embedded In-Memory Derby database in my web application. I found a reference on Oracle's web site that states you can initialize a database with DDL and DML statements using properties like the following:

>         <properties>
>             <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
>             <property name="javax.persistence.schema-generation.create-source" value="script"/>
>             <property name="javax.persistence.schema-generation.create-script-source" value="META-INF/sql/create.sql" />
>             <property name="javax.persistence.sql-load-script-source" value="META-INF/sql/data.sql" />
>         </properties>


However, that seems to be a new feature in JPA 2.1 spec as part of JEE 6. I am working with OpenJPA provided by Apache TomEE, which is openjpa-2.4.0 non-final, a JPA 2.0 implementation I would imagine. The current release on OpenJPA web site is openjpa 2.3.

Is there a way to accomplish this via JPA 2.0  and/or OpenJPA properties? I am trying to initialize an in-memory database for a test case I try to provide to someone.

Any help would be much appreciated.

Kay Wrobel

Re: Initialize Database Via Persistence.xml

Posted by Kay Wrobel <ka...@gmx.net>.
I posted in the TomEE group asking for an example of how to use the import.*.sql file. Anybody care to elaborate or point me to a link where this is documented?

Thanks.

Kay

> On Apr 24, 2015, at 2:37 PM, Mark Struberg <st...@yahoo.de> wrote:
> 
> Yes, in that case for sure!
> 
> I just wanted to add this note as there are some people (not you, but others who might read this in the archive later) who just read this and then copy this 1:1 to their productive application… 
> 
> I’ve seen _way_ too many sample code going into production already :(
> 
> LieGrue,
> strub
> 
> 
>> Am 24.04.2015 um 21:20 schrieb Kay Wrobel <ka...@gmx.net>:
>> 
>> Danke, Mark.
>> 
>> This IS for a test case with a limited amount of data. So a perfectly valid approach, I'd say.
>> 
>> Kay
>> 
>>> On Apr 24, 2015, at 2:14 PM, Mark Struberg <st...@yahoo.de> wrote:
>>> 
>>> All this is fine for sample apps and smallish installations. 
>>> 
>>> However, I personally suggest to neither use the import.sql magic nor the auto-generated schema creation in _real_ production. 
>>> 
>>> Why so?
>>> *) Simple because for a real applciation you will later have to add new features, change the db schema and probably also do a migration. 
>>> 
>>> *) For a real application you really like to look at the database. Sometimes you get intermediate mapping tables instead of a simple 1:1 if you don’t take care. My generating the schema upfront you can really look at the create table statements. I alsays use the openjpa-maven-plugin for it. For unit tests I use the sql-maven-plugin to create the test database.
>>> 
>>> *) you might like to use liquibase or flywaydb and a schema-version management. I won’t go into details, but check out those 2 projects. Definitly worth looking at.
>>> 
>>> LieGrue,
>>> strub
>>> 
>>> 
>>>> Am 24.04.2015 um 19:11 schrieb Romain Manni-Bucau <rm...@gmail.com>:
>>>> 
>>>> 2015-04-24 19:01 GMT+02:00 Kay Wrobel <ka...@gmx.net>:
>>>> 
>>>>> Romain: you're right! Derby.jar doesn't ship with TomEE. No clue how that
>>>>> ended up in the lib directory. My mistake.
>>>>> 
>>>>> So you're suggesting I use Hibernate? But I have a Derby database and I
>>>>> extracted the data as INSERT statements (from Squirrel tool) and it seems
>>>>> to contain some Derby-specific stuff, like the notation of date field data
>>>>> looks like this: {d '2015-04-24'}. Not sure if Hibernate will take that or
>>>>> complain. I would prefer the other solution where I can provide an SQL
>>>>> script that is written for that database system (Derby).
>>>>> 
>>>>> 
>>>> import-<name either of the datasource or the persistence unit>.sql is a
>>>> tomee feature not an hibernate one (import.sql).
>>>> 
>>>> PS: if you choose the tomee solution maybe move over tomee@ list
>>>> 
>>>> 
>>>>>> On Apr 24, 2015, at 11:38 AM, Romain Manni-Bucau <rm...@gmail.com>
>>>>> wrote:
>>>>>> 
>>>>>> 2015-04-24 18:15 GMT+02:00 Kay Wrobel <ka...@gmx.net>:
>>>>>> 
>>>>>>> Re your question about the special need: yes. I want to provide a full
>>>>>>> database, the SAMPLE Derby database that ships with NetBeans/Glassfish
>>>>> to a
>>>>>>> user who will be testing a specific use case, but under Apache TomEE.
>>>>>>> Originally I wanted to provide the entire database as a Derby Database
>>>>>>> file/directory inside my web app and then connect to it via the Derby
>>>>>>> Embedded driver. I fail to be able to provide the proper JDBC URL so
>>>>> that
>>>>>>> TomEE finds that embedded database in my web app directory (not sure
>>>>> where
>>>>>>> TomEE even looks at.
>>>>>>> 
>>>>>>> So I decided, well, I would set the database URL to
>>>>>>> jdbc:derby:memory:sample;create=true as an in-memory database and simply
>>>>>>> run an SQL script that completely creates the database with CREATE TABLE
>>>>>>> and INSERT statements. But I didn't know how to instruct the persistence
>>>>>>> layer to run that script when the application launches. Which is when I
>>>>>>> found the Oracle reference I mentioned earlier, but it is part of JPA
>>>>> 2.1
>>>>>>> as part of the JEE 7 spec. TomEE is based on JEE 6 and JPA 2.0, which
>>>>>>> apparently doesn't have that feature. Which is why I turned to the
>>>>> OpenJPA
>>>>>>> community to see if that is something OpenJPA can do. Apparently this
>>>>> has
>>>>>>> been something EclipseLink and Hibernate had figured out a while ago,
>>>>> but I
>>>>>>> want to go with what TomEE provides, which is OpenJPA. TomEE also ships
>>>>>>> with the derby.jar, so there's that.
>>>>>>> 
>>>>>>> 
>>>>>> TomEE doesnt provide derby but hsqldb. In TomEE you can use
>>>>>> import-<datasource>.sql or import-<persistenceunit>.sql init scripts for
>>>>>> instance. ALso tomee datasource config (commons-bdcp) has such an option.
>>>>>> Can be easier alternative to jpa config.
>>>>>> 
>>>>>> 
>>>>>>> Please advise.
>>>>>>> 
>>>>>>> Kay
>>>>>>> 
>>>>>>>> On Apr 24, 2015, at 10:59 AM, Jody Grassel <fy...@gmail.com> wrote:
>>>>>>>> 
>>>>>>>> That directive will instruct OpenJPA to introspect the databases to
>>>>>>> ensure
>>>>>>>> the tables needed for the table schema defined by your ORM exists, and
>>>>>>> will
>>>>>>>> create the table structures automatically if they do not exist.  Do you
>>>>>>>> have a special need that requires OpenJPA to execute a provided SQL
>>>>>>> script?
>>>>>>>> 
>>>>>>>> On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net>
>>>>> wrote:
>>>>>>>> 
>>>>>>>>> Thanks, Jody. How will that let me provide an SQL script containing
>>>>>>> CREATE
>>>>>>>>> TABLE and INSERT statements?
>>>>>>>>> 
>>>>>>>>> Kay
>>>>>>>>> 
>>>>>>>>>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com>
>>>>> wrote:
>>>>>>>>>> 
>>>>>>>>>> Add the following property to your persistence unit:
>>>>>>>>>> 
>>>>>>>>>> <property name="openjpa.jdbc.SynchronizeMappings"
>>>>>>>>>> value="buildSchema(ForeignKeys=true)"/>
>>>>>>>>>> 
>>>>>>>>>> 
>>>>>>>>>> 
>>>>>>>>>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net>
>>>>>>> wrote:
>>>>>>>>>> 
>>>>>>>>>>> Hi everybody.
>>>>>>>>>>> 
>>>>>>>>>>> I am having a rough time finding a way to initialize an Embedded
>>>>>>>>> In-Memory
>>>>>>>>>>> Derby database in my web application. I found a reference on
>>>>> Oracle's
>>>>>>>>> web
>>>>>>>>>>> site that states you can initialize a database with DDL and DML
>>>>>>>>> statements
>>>>>>>>>>> using properties like the following:
>>>>>>>>>>> 
>>>>>>>>>>>>  <properties>
>>>>>>>>>>>>      <property
>>>>>>>>>>> name="javax.persistence.schema-generation.database.action"
>>>>>>>>>>> value="drop-and-create"/>
>>>>>>>>>>>>      <property
>>>>>>>>>>> name="javax.persistence.schema-generation.create-source"
>>>>>>>>> value="script"/>
>>>>>>>>>>>>      <property
>>>>>>>>>>> name="javax.persistence.schema-generation.create-script-source"
>>>>>>>>>>> value="META-INF/sql/create.sql" />
>>>>>>>>>>>>      <property name="javax.persistence.sql-load-script-source"
>>>>>>>>>>> value="META-INF/sql/data.sql" />
>>>>>>>>>>>>  </properties>
>>>>>>>>>>> 
>>>>>>>>>>> 
>>>>>>>>>>> However, that seems to be a new feature in JPA 2.1 spec as part of
>>>>> JEE
>>>>>>>>> 6.
>>>>>>>>>>> I am working with OpenJPA provided by Apache TomEE, which is
>>>>>>>>> openjpa-2.4.0
>>>>>>>>>>> non-final, a JPA 2.0 implementation I would imagine. The current
>>>>>>>>> release on
>>>>>>>>>>> OpenJPA web site is openjpa 2.3.
>>>>>>>>>>> 
>>>>>>>>>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
>>>>>>>>> properties?
>>>>>>>>>>> I am trying to initialize an in-memory database for a test case I
>>>>> try
>>>>>>> to
>>>>>>>>>>> provide to someone.
>>>>>>>>>>> 
>>>>>>>>>>> Any help would be much appreciated.
>>>>>>>>>>> 
>>>>>>>>>>> Kay Wrobel
>>>>>>>>> 
>>>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>> 
>>>>> 
>>> 
>> 
> 


Re: Initialize Database Via Persistence.xml

Posted by Mark Struberg <st...@yahoo.de>.
Yes, in that case for sure!

I just wanted to add this note as there are some people (not you, but others who might read this in the archive later) who just read this and then copy this 1:1 to their productive application… 

I’ve seen _way_ too many sample code going into production already :(

LieGrue,
strub


> Am 24.04.2015 um 21:20 schrieb Kay Wrobel <ka...@gmx.net>:
> 
> Danke, Mark.
> 
> This IS for a test case with a limited amount of data. So a perfectly valid approach, I'd say.
> 
> Kay
> 
>> On Apr 24, 2015, at 2:14 PM, Mark Struberg <st...@yahoo.de> wrote:
>> 
>> All this is fine for sample apps and smallish installations. 
>> 
>> However, I personally suggest to neither use the import.sql magic nor the auto-generated schema creation in _real_ production. 
>> 
>> Why so?
>> *) Simple because for a real applciation you will later have to add new features, change the db schema and probably also do a migration. 
>> 
>> *) For a real application you really like to look at the database. Sometimes you get intermediate mapping tables instead of a simple 1:1 if you don’t take care. My generating the schema upfront you can really look at the create table statements. I alsays use the openjpa-maven-plugin for it. For unit tests I use the sql-maven-plugin to create the test database.
>> 
>> *) you might like to use liquibase or flywaydb and a schema-version management. I won’t go into details, but check out those 2 projects. Definitly worth looking at.
>> 
>> LieGrue,
>> strub
>> 
>> 
>>> Am 24.04.2015 um 19:11 schrieb Romain Manni-Bucau <rm...@gmail.com>:
>>> 
>>> 2015-04-24 19:01 GMT+02:00 Kay Wrobel <ka...@gmx.net>:
>>> 
>>>> Romain: you're right! Derby.jar doesn't ship with TomEE. No clue how that
>>>> ended up in the lib directory. My mistake.
>>>> 
>>>> So you're suggesting I use Hibernate? But I have a Derby database and I
>>>> extracted the data as INSERT statements (from Squirrel tool) and it seems
>>>> to contain some Derby-specific stuff, like the notation of date field data
>>>> looks like this: {d '2015-04-24'}. Not sure if Hibernate will take that or
>>>> complain. I would prefer the other solution where I can provide an SQL
>>>> script that is written for that database system (Derby).
>>>> 
>>>> 
>>> import-<name either of the datasource or the persistence unit>.sql is a
>>> tomee feature not an hibernate one (import.sql).
>>> 
>>> PS: if you choose the tomee solution maybe move over tomee@ list
>>> 
>>> 
>>>>> On Apr 24, 2015, at 11:38 AM, Romain Manni-Bucau <rm...@gmail.com>
>>>> wrote:
>>>>> 
>>>>> 2015-04-24 18:15 GMT+02:00 Kay Wrobel <ka...@gmx.net>:
>>>>> 
>>>>>> Re your question about the special need: yes. I want to provide a full
>>>>>> database, the SAMPLE Derby database that ships with NetBeans/Glassfish
>>>> to a
>>>>>> user who will be testing a specific use case, but under Apache TomEE.
>>>>>> Originally I wanted to provide the entire database as a Derby Database
>>>>>> file/directory inside my web app and then connect to it via the Derby
>>>>>> Embedded driver. I fail to be able to provide the proper JDBC URL so
>>>> that
>>>>>> TomEE finds that embedded database in my web app directory (not sure
>>>> where
>>>>>> TomEE even looks at.
>>>>>> 
>>>>>> So I decided, well, I would set the database URL to
>>>>>> jdbc:derby:memory:sample;create=true as an in-memory database and simply
>>>>>> run an SQL script that completely creates the database with CREATE TABLE
>>>>>> and INSERT statements. But I didn't know how to instruct the persistence
>>>>>> layer to run that script when the application launches. Which is when I
>>>>>> found the Oracle reference I mentioned earlier, but it is part of JPA
>>>> 2.1
>>>>>> as part of the JEE 7 spec. TomEE is based on JEE 6 and JPA 2.0, which
>>>>>> apparently doesn't have that feature. Which is why I turned to the
>>>> OpenJPA
>>>>>> community to see if that is something OpenJPA can do. Apparently this
>>>> has
>>>>>> been something EclipseLink and Hibernate had figured out a while ago,
>>>> but I
>>>>>> want to go with what TomEE provides, which is OpenJPA. TomEE also ships
>>>>>> with the derby.jar, so there's that.
>>>>>> 
>>>>>> 
>>>>> TomEE doesnt provide derby but hsqldb. In TomEE you can use
>>>>> import-<datasource>.sql or import-<persistenceunit>.sql init scripts for
>>>>> instance. ALso tomee datasource config (commons-bdcp) has such an option.
>>>>> Can be easier alternative to jpa config.
>>>>> 
>>>>> 
>>>>>> Please advise.
>>>>>> 
>>>>>> Kay
>>>>>> 
>>>>>>> On Apr 24, 2015, at 10:59 AM, Jody Grassel <fy...@gmail.com> wrote:
>>>>>>> 
>>>>>>> That directive will instruct OpenJPA to introspect the databases to
>>>>>> ensure
>>>>>>> the tables needed for the table schema defined by your ORM exists, and
>>>>>> will
>>>>>>> create the table structures automatically if they do not exist.  Do you
>>>>>>> have a special need that requires OpenJPA to execute a provided SQL
>>>>>> script?
>>>>>>> 
>>>>>>> On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net>
>>>> wrote:
>>>>>>> 
>>>>>>>> Thanks, Jody. How will that let me provide an SQL script containing
>>>>>> CREATE
>>>>>>>> TABLE and INSERT statements?
>>>>>>>> 
>>>>>>>> Kay
>>>>>>>> 
>>>>>>>>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com>
>>>> wrote:
>>>>>>>>> 
>>>>>>>>> Add the following property to your persistence unit:
>>>>>>>>> 
>>>>>>>>> <property name="openjpa.jdbc.SynchronizeMappings"
>>>>>>>>> value="buildSchema(ForeignKeys=true)"/>
>>>>>>>>> 
>>>>>>>>> 
>>>>>>>>> 
>>>>>>>>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net>
>>>>>> wrote:
>>>>>>>>> 
>>>>>>>>>> Hi everybody.
>>>>>>>>>> 
>>>>>>>>>> I am having a rough time finding a way to initialize an Embedded
>>>>>>>> In-Memory
>>>>>>>>>> Derby database in my web application. I found a reference on
>>>> Oracle's
>>>>>>>> web
>>>>>>>>>> site that states you can initialize a database with DDL and DML
>>>>>>>> statements
>>>>>>>>>> using properties like the following:
>>>>>>>>>> 
>>>>>>>>>>>   <properties>
>>>>>>>>>>>       <property
>>>>>>>>>> name="javax.persistence.schema-generation.database.action"
>>>>>>>>>> value="drop-and-create"/>
>>>>>>>>>>>       <property
>>>>>>>>>> name="javax.persistence.schema-generation.create-source"
>>>>>>>> value="script"/>
>>>>>>>>>>>       <property
>>>>>>>>>> name="javax.persistence.schema-generation.create-script-source"
>>>>>>>>>> value="META-INF/sql/create.sql" />
>>>>>>>>>>>       <property name="javax.persistence.sql-load-script-source"
>>>>>>>>>> value="META-INF/sql/data.sql" />
>>>>>>>>>>>   </properties>
>>>>>>>>>> 
>>>>>>>>>> 
>>>>>>>>>> However, that seems to be a new feature in JPA 2.1 spec as part of
>>>> JEE
>>>>>>>> 6.
>>>>>>>>>> I am working with OpenJPA provided by Apache TomEE, which is
>>>>>>>> openjpa-2.4.0
>>>>>>>>>> non-final, a JPA 2.0 implementation I would imagine. The current
>>>>>>>> release on
>>>>>>>>>> OpenJPA web site is openjpa 2.3.
>>>>>>>>>> 
>>>>>>>>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
>>>>>>>> properties?
>>>>>>>>>> I am trying to initialize an in-memory database for a test case I
>>>> try
>>>>>> to
>>>>>>>>>> provide to someone.
>>>>>>>>>> 
>>>>>>>>>> Any help would be much appreciated.
>>>>>>>>>> 
>>>>>>>>>> Kay Wrobel
>>>>>>>> 
>>>>>>>> 
>>>>>> 
>>>>>> 
>>>> 
>>>> 
>> 
> 


Re: Initialize Database Via Persistence.xml

Posted by Kay Wrobel <ka...@gmx.net>.
Danke, Mark.

This IS for a test case with a limited amount of data. So a perfectly valid approach, I'd say.

Kay

> On Apr 24, 2015, at 2:14 PM, Mark Struberg <st...@yahoo.de> wrote:
> 
> All this is fine for sample apps and smallish installations. 
> 
> However, I personally suggest to neither use the import.sql magic nor the auto-generated schema creation in _real_ production. 
> 
> Why so?
> *) Simple because for a real applciation you will later have to add new features, change the db schema and probably also do a migration. 
> 
> *) For a real application you really like to look at the database. Sometimes you get intermediate mapping tables instead of a simple 1:1 if you don’t take care. My generating the schema upfront you can really look at the create table statements. I alsays use the openjpa-maven-plugin for it. For unit tests I use the sql-maven-plugin to create the test database.
> 
> *) you might like to use liquibase or flywaydb and a schema-version management. I won’t go into details, but check out those 2 projects. Definitly worth looking at.
> 
> LieGrue,
> strub
> 
> 
>> Am 24.04.2015 um 19:11 schrieb Romain Manni-Bucau <rm...@gmail.com>:
>> 
>> 2015-04-24 19:01 GMT+02:00 Kay Wrobel <ka...@gmx.net>:
>> 
>>> Romain: you're right! Derby.jar doesn't ship with TomEE. No clue how that
>>> ended up in the lib directory. My mistake.
>>> 
>>> So you're suggesting I use Hibernate? But I have a Derby database and I
>>> extracted the data as INSERT statements (from Squirrel tool) and it seems
>>> to contain some Derby-specific stuff, like the notation of date field data
>>> looks like this: {d '2015-04-24'}. Not sure if Hibernate will take that or
>>> complain. I would prefer the other solution where I can provide an SQL
>>> script that is written for that database system (Derby).
>>> 
>>> 
>> import-<name either of the datasource or the persistence unit>.sql is a
>> tomee feature not an hibernate one (import.sql).
>> 
>> PS: if you choose the tomee solution maybe move over tomee@ list
>> 
>> 
>>>> On Apr 24, 2015, at 11:38 AM, Romain Manni-Bucau <rm...@gmail.com>
>>> wrote:
>>>> 
>>>> 2015-04-24 18:15 GMT+02:00 Kay Wrobel <ka...@gmx.net>:
>>>> 
>>>>> Re your question about the special need: yes. I want to provide a full
>>>>> database, the SAMPLE Derby database that ships with NetBeans/Glassfish
>>> to a
>>>>> user who will be testing a specific use case, but under Apache TomEE.
>>>>> Originally I wanted to provide the entire database as a Derby Database
>>>>> file/directory inside my web app and then connect to it via the Derby
>>>>> Embedded driver. I fail to be able to provide the proper JDBC URL so
>>> that
>>>>> TomEE finds that embedded database in my web app directory (not sure
>>> where
>>>>> TomEE even looks at.
>>>>> 
>>>>> So I decided, well, I would set the database URL to
>>>>> jdbc:derby:memory:sample;create=true as an in-memory database and simply
>>>>> run an SQL script that completely creates the database with CREATE TABLE
>>>>> and INSERT statements. But I didn't know how to instruct the persistence
>>>>> layer to run that script when the application launches. Which is when I
>>>>> found the Oracle reference I mentioned earlier, but it is part of JPA
>>> 2.1
>>>>> as part of the JEE 7 spec. TomEE is based on JEE 6 and JPA 2.0, which
>>>>> apparently doesn't have that feature. Which is why I turned to the
>>> OpenJPA
>>>>> community to see if that is something OpenJPA can do. Apparently this
>>> has
>>>>> been something EclipseLink and Hibernate had figured out a while ago,
>>> but I
>>>>> want to go with what TomEE provides, which is OpenJPA. TomEE also ships
>>>>> with the derby.jar, so there's that.
>>>>> 
>>>>> 
>>>> TomEE doesnt provide derby but hsqldb. In TomEE you can use
>>>> import-<datasource>.sql or import-<persistenceunit>.sql init scripts for
>>>> instance. ALso tomee datasource config (commons-bdcp) has such an option.
>>>> Can be easier alternative to jpa config.
>>>> 
>>>> 
>>>>> Please advise.
>>>>> 
>>>>> Kay
>>>>> 
>>>>>> On Apr 24, 2015, at 10:59 AM, Jody Grassel <fy...@gmail.com> wrote:
>>>>>> 
>>>>>> That directive will instruct OpenJPA to introspect the databases to
>>>>> ensure
>>>>>> the tables needed for the table schema defined by your ORM exists, and
>>>>> will
>>>>>> create the table structures automatically if they do not exist.  Do you
>>>>>> have a special need that requires OpenJPA to execute a provided SQL
>>>>> script?
>>>>>> 
>>>>>> On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net>
>>> wrote:
>>>>>> 
>>>>>>> Thanks, Jody. How will that let me provide an SQL script containing
>>>>> CREATE
>>>>>>> TABLE and INSERT statements?
>>>>>>> 
>>>>>>> Kay
>>>>>>> 
>>>>>>>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com>
>>> wrote:
>>>>>>>> 
>>>>>>>> Add the following property to your persistence unit:
>>>>>>>> 
>>>>>>>> <property name="openjpa.jdbc.SynchronizeMappings"
>>>>>>>> value="buildSchema(ForeignKeys=true)"/>
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net>
>>>>> wrote:
>>>>>>>> 
>>>>>>>>> Hi everybody.
>>>>>>>>> 
>>>>>>>>> I am having a rough time finding a way to initialize an Embedded
>>>>>>> In-Memory
>>>>>>>>> Derby database in my web application. I found a reference on
>>> Oracle's
>>>>>>> web
>>>>>>>>> site that states you can initialize a database with DDL and DML
>>>>>>> statements
>>>>>>>>> using properties like the following:
>>>>>>>>> 
>>>>>>>>>>    <properties>
>>>>>>>>>>        <property
>>>>>>>>> name="javax.persistence.schema-generation.database.action"
>>>>>>>>> value="drop-and-create"/>
>>>>>>>>>>        <property
>>>>>>>>> name="javax.persistence.schema-generation.create-source"
>>>>>>> value="script"/>
>>>>>>>>>>        <property
>>>>>>>>> name="javax.persistence.schema-generation.create-script-source"
>>>>>>>>> value="META-INF/sql/create.sql" />
>>>>>>>>>>        <property name="javax.persistence.sql-load-script-source"
>>>>>>>>> value="META-INF/sql/data.sql" />
>>>>>>>>>>    </properties>
>>>>>>>>> 
>>>>>>>>> 
>>>>>>>>> However, that seems to be a new feature in JPA 2.1 spec as part of
>>> JEE
>>>>>>> 6.
>>>>>>>>> I am working with OpenJPA provided by Apache TomEE, which is
>>>>>>> openjpa-2.4.0
>>>>>>>>> non-final, a JPA 2.0 implementation I would imagine. The current
>>>>>>> release on
>>>>>>>>> OpenJPA web site is openjpa 2.3.
>>>>>>>>> 
>>>>>>>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
>>>>>>> properties?
>>>>>>>>> I am trying to initialize an in-memory database for a test case I
>>> try
>>>>> to
>>>>>>>>> provide to someone.
>>>>>>>>> 
>>>>>>>>> Any help would be much appreciated.
>>>>>>>>> 
>>>>>>>>> Kay Wrobel
>>>>>>> 
>>>>>>> 
>>>>> 
>>>>> 
>>> 
>>> 
> 


Re: Initialize Database Via Persistence.xml

Posted by Mark Struberg <st...@yahoo.de>.
All this is fine for sample apps and smallish installations. 

However, I personally suggest to neither use the import.sql magic nor the auto-generated schema creation in _real_ production. 

Why so?
*) Simple because for a real applciation you will later have to add new features, change the db schema and probably also do a migration. 

*) For a real application you really like to look at the database. Sometimes you get intermediate mapping tables instead of a simple 1:1 if you don’t take care. My generating the schema upfront you can really look at the create table statements. I alsays use the openjpa-maven-plugin for it. For unit tests I use the sql-maven-plugin to create the test database.

*) you might like to use liquibase or flywaydb and a schema-version management. I won’t go into details, but check out those 2 projects. Definitly worth looking at.

LieGrue,
strub


> Am 24.04.2015 um 19:11 schrieb Romain Manni-Bucau <rm...@gmail.com>:
> 
> 2015-04-24 19:01 GMT+02:00 Kay Wrobel <ka...@gmx.net>:
> 
>> Romain: you're right! Derby.jar doesn't ship with TomEE. No clue how that
>> ended up in the lib directory. My mistake.
>> 
>> So you're suggesting I use Hibernate? But I have a Derby database and I
>> extracted the data as INSERT statements (from Squirrel tool) and it seems
>> to contain some Derby-specific stuff, like the notation of date field data
>> looks like this: {d '2015-04-24'}. Not sure if Hibernate will take that or
>> complain. I would prefer the other solution where I can provide an SQL
>> script that is written for that database system (Derby).
>> 
>> 
> import-<name either of the datasource or the persistence unit>.sql is a
> tomee feature not an hibernate one (import.sql).
> 
> PS: if you choose the tomee solution maybe move over tomee@ list
> 
> 
>>> On Apr 24, 2015, at 11:38 AM, Romain Manni-Bucau <rm...@gmail.com>
>> wrote:
>>> 
>>> 2015-04-24 18:15 GMT+02:00 Kay Wrobel <ka...@gmx.net>:
>>> 
>>>> Re your question about the special need: yes. I want to provide a full
>>>> database, the SAMPLE Derby database that ships with NetBeans/Glassfish
>> to a
>>>> user who will be testing a specific use case, but under Apache TomEE.
>>>> Originally I wanted to provide the entire database as a Derby Database
>>>> file/directory inside my web app and then connect to it via the Derby
>>>> Embedded driver. I fail to be able to provide the proper JDBC URL so
>> that
>>>> TomEE finds that embedded database in my web app directory (not sure
>> where
>>>> TomEE even looks at.
>>>> 
>>>> So I decided, well, I would set the database URL to
>>>> jdbc:derby:memory:sample;create=true as an in-memory database and simply
>>>> run an SQL script that completely creates the database with CREATE TABLE
>>>> and INSERT statements. But I didn't know how to instruct the persistence
>>>> layer to run that script when the application launches. Which is when I
>>>> found the Oracle reference I mentioned earlier, but it is part of JPA
>> 2.1
>>>> as part of the JEE 7 spec. TomEE is based on JEE 6 and JPA 2.0, which
>>>> apparently doesn't have that feature. Which is why I turned to the
>> OpenJPA
>>>> community to see if that is something OpenJPA can do. Apparently this
>> has
>>>> been something EclipseLink and Hibernate had figured out a while ago,
>> but I
>>>> want to go with what TomEE provides, which is OpenJPA. TomEE also ships
>>>> with the derby.jar, so there's that.
>>>> 
>>>> 
>>> TomEE doesnt provide derby but hsqldb. In TomEE you can use
>>> import-<datasource>.sql or import-<persistenceunit>.sql init scripts for
>>> instance. ALso tomee datasource config (commons-bdcp) has such an option.
>>> Can be easier alternative to jpa config.
>>> 
>>> 
>>>> Please advise.
>>>> 
>>>> Kay
>>>> 
>>>>> On Apr 24, 2015, at 10:59 AM, Jody Grassel <fy...@gmail.com> wrote:
>>>>> 
>>>>> That directive will instruct OpenJPA to introspect the databases to
>>>> ensure
>>>>> the tables needed for the table schema defined by your ORM exists, and
>>>> will
>>>>> create the table structures automatically if they do not exist.  Do you
>>>>> have a special need that requires OpenJPA to execute a provided SQL
>>>> script?
>>>>> 
>>>>> On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net>
>> wrote:
>>>>> 
>>>>>> Thanks, Jody. How will that let me provide an SQL script containing
>>>> CREATE
>>>>>> TABLE and INSERT statements?
>>>>>> 
>>>>>> Kay
>>>>>> 
>>>>>>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com>
>> wrote:
>>>>>>> 
>>>>>>> Add the following property to your persistence unit:
>>>>>>> 
>>>>>>> <property name="openjpa.jdbc.SynchronizeMappings"
>>>>>>> value="buildSchema(ForeignKeys=true)"/>
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net>
>>>> wrote:
>>>>>>> 
>>>>>>>> Hi everybody.
>>>>>>>> 
>>>>>>>> I am having a rough time finding a way to initialize an Embedded
>>>>>> In-Memory
>>>>>>>> Derby database in my web application. I found a reference on
>> Oracle's
>>>>>> web
>>>>>>>> site that states you can initialize a database with DDL and DML
>>>>>> statements
>>>>>>>> using properties like the following:
>>>>>>>> 
>>>>>>>>>     <properties>
>>>>>>>>>         <property
>>>>>>>> name="javax.persistence.schema-generation.database.action"
>>>>>>>> value="drop-and-create"/>
>>>>>>>>>         <property
>>>>>>>> name="javax.persistence.schema-generation.create-source"
>>>>>> value="script"/>
>>>>>>>>>         <property
>>>>>>>> name="javax.persistence.schema-generation.create-script-source"
>>>>>>>> value="META-INF/sql/create.sql" />
>>>>>>>>>         <property name="javax.persistence.sql-load-script-source"
>>>>>>>> value="META-INF/sql/data.sql" />
>>>>>>>>>     </properties>
>>>>>>>> 
>>>>>>>> 
>>>>>>>> However, that seems to be a new feature in JPA 2.1 spec as part of
>> JEE
>>>>>> 6.
>>>>>>>> I am working with OpenJPA provided by Apache TomEE, which is
>>>>>> openjpa-2.4.0
>>>>>>>> non-final, a JPA 2.0 implementation I would imagine. The current
>>>>>> release on
>>>>>>>> OpenJPA web site is openjpa 2.3.
>>>>>>>> 
>>>>>>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
>>>>>> properties?
>>>>>>>> I am trying to initialize an in-memory database for a test case I
>> try
>>>> to
>>>>>>>> provide to someone.
>>>>>>>> 
>>>>>>>> Any help would be much appreciated.
>>>>>>>> 
>>>>>>>> Kay Wrobel
>>>>>> 
>>>>>> 
>>>> 
>>>> 
>> 
>> 


Re: Initialize Database Via Persistence.xml

Posted by Romain Manni-Bucau <rm...@gmail.com>.
2015-04-24 19:01 GMT+02:00 Kay Wrobel <ka...@gmx.net>:

> Romain: you're right! Derby.jar doesn't ship with TomEE. No clue how that
> ended up in the lib directory. My mistake.
>
> So you're suggesting I use Hibernate? But I have a Derby database and I
> extracted the data as INSERT statements (from Squirrel tool) and it seems
> to contain some Derby-specific stuff, like the notation of date field data
> looks like this: {d '2015-04-24'}. Not sure if Hibernate will take that or
> complain. I would prefer the other solution where I can provide an SQL
> script that is written for that database system (Derby).
>
>
import-<name either of the datasource or the persistence unit>.sql is a
tomee feature not an hibernate one (import.sql).

PS: if you choose the tomee solution maybe move over tomee@ list


> > On Apr 24, 2015, at 11:38 AM, Romain Manni-Bucau <rm...@gmail.com>
> wrote:
> >
> > 2015-04-24 18:15 GMT+02:00 Kay Wrobel <ka...@gmx.net>:
> >
> >> Re your question about the special need: yes. I want to provide a full
> >> database, the SAMPLE Derby database that ships with NetBeans/Glassfish
> to a
> >> user who will be testing a specific use case, but under Apache TomEE.
> >> Originally I wanted to provide the entire database as a Derby Database
> >> file/directory inside my web app and then connect to it via the Derby
> >> Embedded driver. I fail to be able to provide the proper JDBC URL so
> that
> >> TomEE finds that embedded database in my web app directory (not sure
> where
> >> TomEE even looks at.
> >>
> >> So I decided, well, I would set the database URL to
> >> jdbc:derby:memory:sample;create=true as an in-memory database and simply
> >> run an SQL script that completely creates the database with CREATE TABLE
> >> and INSERT statements. But I didn't know how to instruct the persistence
> >> layer to run that script when the application launches. Which is when I
> >> found the Oracle reference I mentioned earlier, but it is part of JPA
> 2.1
> >> as part of the JEE 7 spec. TomEE is based on JEE 6 and JPA 2.0, which
> >> apparently doesn't have that feature. Which is why I turned to the
> OpenJPA
> >> community to see if that is something OpenJPA can do. Apparently this
> has
> >> been something EclipseLink and Hibernate had figured out a while ago,
> but I
> >> want to go with what TomEE provides, which is OpenJPA. TomEE also ships
> >> with the derby.jar, so there's that.
> >>
> >>
> > TomEE doesnt provide derby but hsqldb. In TomEE you can use
> > import-<datasource>.sql or import-<persistenceunit>.sql init scripts for
> > instance. ALso tomee datasource config (commons-bdcp) has such an option.
> > Can be easier alternative to jpa config.
> >
> >
> >> Please advise.
> >>
> >> Kay
> >>
> >>> On Apr 24, 2015, at 10:59 AM, Jody Grassel <fy...@gmail.com> wrote:
> >>>
> >>> That directive will instruct OpenJPA to introspect the databases to
> >> ensure
> >>> the tables needed for the table schema defined by your ORM exists, and
> >> will
> >>> create the table structures automatically if they do not exist.  Do you
> >>> have a special need that requires OpenJPA to execute a provided SQL
> >> script?
> >>>
> >>> On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net>
> wrote:
> >>>
> >>>> Thanks, Jody. How will that let me provide an SQL script containing
> >> CREATE
> >>>> TABLE and INSERT statements?
> >>>>
> >>>> Kay
> >>>>
> >>>>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com>
> wrote:
> >>>>>
> >>>>> Add the following property to your persistence unit:
> >>>>>
> >>>>> <property name="openjpa.jdbc.SynchronizeMappings"
> >>>>> value="buildSchema(ForeignKeys=true)"/>
> >>>>>
> >>>>>
> >>>>>
> >>>>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net>
> >> wrote:
> >>>>>
> >>>>>> Hi everybody.
> >>>>>>
> >>>>>> I am having a rough time finding a way to initialize an Embedded
> >>>> In-Memory
> >>>>>> Derby database in my web application. I found a reference on
> Oracle's
> >>>> web
> >>>>>> site that states you can initialize a database with DDL and DML
> >>>> statements
> >>>>>> using properties like the following:
> >>>>>>
> >>>>>>>      <properties>
> >>>>>>>          <property
> >>>>>> name="javax.persistence.schema-generation.database.action"
> >>>>>> value="drop-and-create"/>
> >>>>>>>          <property
> >>>>>> name="javax.persistence.schema-generation.create-source"
> >>>> value="script"/>
> >>>>>>>          <property
> >>>>>> name="javax.persistence.schema-generation.create-script-source"
> >>>>>> value="META-INF/sql/create.sql" />
> >>>>>>>          <property name="javax.persistence.sql-load-script-source"
> >>>>>> value="META-INF/sql/data.sql" />
> >>>>>>>      </properties>
> >>>>>>
> >>>>>>
> >>>>>> However, that seems to be a new feature in JPA 2.1 spec as part of
> JEE
> >>>> 6.
> >>>>>> I am working with OpenJPA provided by Apache TomEE, which is
> >>>> openjpa-2.4.0
> >>>>>> non-final, a JPA 2.0 implementation I would imagine. The current
> >>>> release on
> >>>>>> OpenJPA web site is openjpa 2.3.
> >>>>>>
> >>>>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
> >>>> properties?
> >>>>>> I am trying to initialize an in-memory database for a test case I
> try
> >> to
> >>>>>> provide to someone.
> >>>>>>
> >>>>>> Any help would be much appreciated.
> >>>>>>
> >>>>>> Kay Wrobel
> >>>>
> >>>>
> >>
> >>
>
>

Re: Initialize Database Via Persistence.xml

Posted by Kay Wrobel <ka...@gmx.net>.
Romain: you're right! Derby.jar doesn't ship with TomEE. No clue how that ended up in the lib directory. My mistake.

So you're suggesting I use Hibernate? But I have a Derby database and I extracted the data as INSERT statements (from Squirrel tool) and it seems to contain some Derby-specific stuff, like the notation of date field data looks like this: {d '2015-04-24'}. Not sure if Hibernate will take that or complain. I would prefer the other solution where I can provide an SQL script that is written for that database system (Derby).

> On Apr 24, 2015, at 11:38 AM, Romain Manni-Bucau <rm...@gmail.com> wrote:
> 
> 2015-04-24 18:15 GMT+02:00 Kay Wrobel <ka...@gmx.net>:
> 
>> Re your question about the special need: yes. I want to provide a full
>> database, the SAMPLE Derby database that ships with NetBeans/Glassfish to a
>> user who will be testing a specific use case, but under Apache TomEE.
>> Originally I wanted to provide the entire database as a Derby Database
>> file/directory inside my web app and then connect to it via the Derby
>> Embedded driver. I fail to be able to provide the proper JDBC URL so that
>> TomEE finds that embedded database in my web app directory (not sure where
>> TomEE even looks at.
>> 
>> So I decided, well, I would set the database URL to
>> jdbc:derby:memory:sample;create=true as an in-memory database and simply
>> run an SQL script that completely creates the database with CREATE TABLE
>> and INSERT statements. But I didn't know how to instruct the persistence
>> layer to run that script when the application launches. Which is when I
>> found the Oracle reference I mentioned earlier, but it is part of JPA 2.1
>> as part of the JEE 7 spec. TomEE is based on JEE 6 and JPA 2.0, which
>> apparently doesn't have that feature. Which is why I turned to the OpenJPA
>> community to see if that is something OpenJPA can do. Apparently this has
>> been something EclipseLink and Hibernate had figured out a while ago, but I
>> want to go with what TomEE provides, which is OpenJPA. TomEE also ships
>> with the derby.jar, so there's that.
>> 
>> 
> TomEE doesnt provide derby but hsqldb. In TomEE you can use
> import-<datasource>.sql or import-<persistenceunit>.sql init scripts for
> instance. ALso tomee datasource config (commons-bdcp) has such an option.
> Can be easier alternative to jpa config.
> 
> 
>> Please advise.
>> 
>> Kay
>> 
>>> On Apr 24, 2015, at 10:59 AM, Jody Grassel <fy...@gmail.com> wrote:
>>> 
>>> That directive will instruct OpenJPA to introspect the databases to
>> ensure
>>> the tables needed for the table schema defined by your ORM exists, and
>> will
>>> create the table structures automatically if they do not exist.  Do you
>>> have a special need that requires OpenJPA to execute a provided SQL
>> script?
>>> 
>>> On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net> wrote:
>>> 
>>>> Thanks, Jody. How will that let me provide an SQL script containing
>> CREATE
>>>> TABLE and INSERT statements?
>>>> 
>>>> Kay
>>>> 
>>>>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com> wrote:
>>>>> 
>>>>> Add the following property to your persistence unit:
>>>>> 
>>>>> <property name="openjpa.jdbc.SynchronizeMappings"
>>>>> value="buildSchema(ForeignKeys=true)"/>
>>>>> 
>>>>> 
>>>>> 
>>>>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net>
>> wrote:
>>>>> 
>>>>>> Hi everybody.
>>>>>> 
>>>>>> I am having a rough time finding a way to initialize an Embedded
>>>> In-Memory
>>>>>> Derby database in my web application. I found a reference on Oracle's
>>>> web
>>>>>> site that states you can initialize a database with DDL and DML
>>>> statements
>>>>>> using properties like the following:
>>>>>> 
>>>>>>>      <properties>
>>>>>>>          <property
>>>>>> name="javax.persistence.schema-generation.database.action"
>>>>>> value="drop-and-create"/>
>>>>>>>          <property
>>>>>> name="javax.persistence.schema-generation.create-source"
>>>> value="script"/>
>>>>>>>          <property
>>>>>> name="javax.persistence.schema-generation.create-script-source"
>>>>>> value="META-INF/sql/create.sql" />
>>>>>>>          <property name="javax.persistence.sql-load-script-source"
>>>>>> value="META-INF/sql/data.sql" />
>>>>>>>      </properties>
>>>>>> 
>>>>>> 
>>>>>> However, that seems to be a new feature in JPA 2.1 spec as part of JEE
>>>> 6.
>>>>>> I am working with OpenJPA provided by Apache TomEE, which is
>>>> openjpa-2.4.0
>>>>>> non-final, a JPA 2.0 implementation I would imagine. The current
>>>> release on
>>>>>> OpenJPA web site is openjpa 2.3.
>>>>>> 
>>>>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
>>>> properties?
>>>>>> I am trying to initialize an in-memory database for a test case I try
>> to
>>>>>> provide to someone.
>>>>>> 
>>>>>> Any help would be much appreciated.
>>>>>> 
>>>>>> Kay Wrobel
>>>> 
>>>> 
>> 
>> 


Re: Initialize Database Via Persistence.xml

Posted by Romain Manni-Bucau <rm...@gmail.com>.
2015-04-24 18:15 GMT+02:00 Kay Wrobel <ka...@gmx.net>:

> Re your question about the special need: yes. I want to provide a full
> database, the SAMPLE Derby database that ships with NetBeans/Glassfish to a
> user who will be testing a specific use case, but under Apache TomEE.
> Originally I wanted to provide the entire database as a Derby Database
> file/directory inside my web app and then connect to it via the Derby
> Embedded driver. I fail to be able to provide the proper JDBC URL so that
> TomEE finds that embedded database in my web app directory (not sure where
> TomEE even looks at.
>
> So I decided, well, I would set the database URL to
> jdbc:derby:memory:sample;create=true as an in-memory database and simply
> run an SQL script that completely creates the database with CREATE TABLE
> and INSERT statements. But I didn't know how to instruct the persistence
> layer to run that script when the application launches. Which is when I
> found the Oracle reference I mentioned earlier, but it is part of JPA 2.1
> as part of the JEE 7 spec. TomEE is based on JEE 6 and JPA 2.0, which
> apparently doesn't have that feature. Which is why I turned to the OpenJPA
> community to see if that is something OpenJPA can do. Apparently this has
> been something EclipseLink and Hibernate had figured out a while ago, but I
> want to go with what TomEE provides, which is OpenJPA. TomEE also ships
> with the derby.jar, so there's that.
>
>
TomEE doesnt provide derby but hsqldb. In TomEE you can use
import-<datasource>.sql or import-<persistenceunit>.sql init scripts for
instance. ALso tomee datasource config (commons-bdcp) has such an option.
Can be easier alternative to jpa config.


> Please advise.
>
> Kay
>
> > On Apr 24, 2015, at 10:59 AM, Jody Grassel <fy...@gmail.com> wrote:
> >
> > That directive will instruct OpenJPA to introspect the databases to
> ensure
> > the tables needed for the table schema defined by your ORM exists, and
> will
> > create the table structures automatically if they do not exist.  Do you
> > have a special need that requires OpenJPA to execute a provided SQL
> script?
> >
> > On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net> wrote:
> >
> >> Thanks, Jody. How will that let me provide an SQL script containing
> CREATE
> >> TABLE and INSERT statements?
> >>
> >> Kay
> >>
> >>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com> wrote:
> >>>
> >>> Add the following property to your persistence unit:
> >>>
> >>> <property name="openjpa.jdbc.SynchronizeMappings"
> >>> value="buildSchema(ForeignKeys=true)"/>
> >>>
> >>>
> >>>
> >>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net>
> wrote:
> >>>
> >>>> Hi everybody.
> >>>>
> >>>> I am having a rough time finding a way to initialize an Embedded
> >> In-Memory
> >>>> Derby database in my web application. I found a reference on Oracle's
> >> web
> >>>> site that states you can initialize a database with DDL and DML
> >> statements
> >>>> using properties like the following:
> >>>>
> >>>>>       <properties>
> >>>>>           <property
> >>>> name="javax.persistence.schema-generation.database.action"
> >>>> value="drop-and-create"/>
> >>>>>           <property
> >>>> name="javax.persistence.schema-generation.create-source"
> >> value="script"/>
> >>>>>           <property
> >>>> name="javax.persistence.schema-generation.create-script-source"
> >>>> value="META-INF/sql/create.sql" />
> >>>>>           <property name="javax.persistence.sql-load-script-source"
> >>>> value="META-INF/sql/data.sql" />
> >>>>>       </properties>
> >>>>
> >>>>
> >>>> However, that seems to be a new feature in JPA 2.1 spec as part of JEE
> >> 6.
> >>>> I am working with OpenJPA provided by Apache TomEE, which is
> >> openjpa-2.4.0
> >>>> non-final, a JPA 2.0 implementation I would imagine. The current
> >> release on
> >>>> OpenJPA web site is openjpa 2.3.
> >>>>
> >>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
> >> properties?
> >>>> I am trying to initialize an in-memory database for a test case I try
> to
> >>>> provide to someone.
> >>>>
> >>>> Any help would be much appreciated.
> >>>>
> >>>> Kay Wrobel
> >>
> >>
>
>

Re: Initialize Database Via Persistence.xml

Posted by Kay Wrobel <ka...@gmx.net>.
Re your question about the special need: yes. I want to provide a full database, the SAMPLE Derby database that ships with NetBeans/Glassfish to a user who will be testing a specific use case, but under Apache TomEE. Originally I wanted to provide the entire database as a Derby Database file/directory inside my web app and then connect to it via the Derby Embedded driver. I fail to be able to provide the proper JDBC URL so that TomEE finds that embedded database in my web app directory (not sure where TomEE even looks at.

So I decided, well, I would set the database URL to jdbc:derby:memory:sample;create=true as an in-memory database and simply run an SQL script that completely creates the database with CREATE TABLE and INSERT statements. But I didn't know how to instruct the persistence layer to run that script when the application launches. Which is when I found the Oracle reference I mentioned earlier, but it is part of JPA 2.1 as part of the JEE 7 spec. TomEE is based on JEE 6 and JPA 2.0, which apparently doesn't have that feature. Which is why I turned to the OpenJPA community to see if that is something OpenJPA can do. Apparently this has been something EclipseLink and Hibernate had figured out a while ago, but I want to go with what TomEE provides, which is OpenJPA. TomEE also ships with the derby.jar, so there's that.

Please advise.

Kay

> On Apr 24, 2015, at 10:59 AM, Jody Grassel <fy...@gmail.com> wrote:
> 
> That directive will instruct OpenJPA to introspect the databases to ensure
> the tables needed for the table schema defined by your ORM exists, and will
> create the table structures automatically if they do not exist.  Do you
> have a special need that requires OpenJPA to execute a provided SQL script?
> 
> On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net> wrote:
> 
>> Thanks, Jody. How will that let me provide an SQL script containing CREATE
>> TABLE and INSERT statements?
>> 
>> Kay
>> 
>>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com> wrote:
>>> 
>>> Add the following property to your persistence unit:
>>> 
>>> <property name="openjpa.jdbc.SynchronizeMappings"
>>> value="buildSchema(ForeignKeys=true)"/>
>>> 
>>> 
>>> 
>>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net> wrote:
>>> 
>>>> Hi everybody.
>>>> 
>>>> I am having a rough time finding a way to initialize an Embedded
>> In-Memory
>>>> Derby database in my web application. I found a reference on Oracle's
>> web
>>>> site that states you can initialize a database with DDL and DML
>> statements
>>>> using properties like the following:
>>>> 
>>>>>       <properties>
>>>>>           <property
>>>> name="javax.persistence.schema-generation.database.action"
>>>> value="drop-and-create"/>
>>>>>           <property
>>>> name="javax.persistence.schema-generation.create-source"
>> value="script"/>
>>>>>           <property
>>>> name="javax.persistence.schema-generation.create-script-source"
>>>> value="META-INF/sql/create.sql" />
>>>>>           <property name="javax.persistence.sql-load-script-source"
>>>> value="META-INF/sql/data.sql" />
>>>>>       </properties>
>>>> 
>>>> 
>>>> However, that seems to be a new feature in JPA 2.1 spec as part of JEE
>> 6.
>>>> I am working with OpenJPA provided by Apache TomEE, which is
>> openjpa-2.4.0
>>>> non-final, a JPA 2.0 implementation I would imagine. The current
>> release on
>>>> OpenJPA web site is openjpa 2.3.
>>>> 
>>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
>> properties?
>>>> I am trying to initialize an in-memory database for a test case I try to
>>>> provide to someone.
>>>> 
>>>> Any help would be much appreciated.
>>>> 
>>>> Kay Wrobel
>> 
>> 


Re: Initialize Database Via Persistence.xml

Posted by Jody Grassel <fy...@gmail.com>.
Yeah, it does.  I don't know if OpenJPA can execute SQL scripts.  It can
certainly generate them for use with other DB tooling, but I am not aware
of any means for the provider to execute SQL scripts as part of the
EntityManager(Factory) bootstrapping.  OpenJPA does ship with a schema tool
(14.1.6.  Schema Tool Ant Task) but that probably won't help you with an
in-memory Derby DB.

I'll look around and consult with my colleagues if this capability exists
in the form you are looking for, but I do not think it does.

On Fri, Apr 24, 2015 at 11:31 AM, Kay Wrobel <ka...@gmx.net> wrote:

> Thanks. I will try that.
>
> I will still be completely stuck populating the database with pre-existing
> "entities", if you know what I mean. I know I could write a class that is
> application-scoped and use the @PostConstruct annotation to do some
> preliminary work, like creating entities using the entity manager. But I
> have like 60-70 records in that database in like 6 or 7 tables and no way
> to turn those database records into Java code that simulated the new
> Entity(x,x,x,x,x); nomenclature. And I really don't want to type all of
> that up. Makes sense?
>
> Kay
>
> > On Apr 24, 2015, at 11:16 AM, Jody Grassel <fy...@gmail.com> wrote:
> >
> > @Size is a bean validation annotation, which is not going to govern
> > schema.  Try adding length=1 to your @Column annotation for discountCode
> >
> > @Id
> > @Basic(optional = false)  // not really necessary as @Ids are always
> > non-optional
> > @NotNull
> > @Size(min = 1, max = 1)
> > @Column(name = "DISCOUNT_CODE"*, length=1*)
> > private String discountCode;
> >
> > On Fri, Apr 24, 2015 at 11:08 AM, Kay Wrobel <ka...@gmx.net> wrote:
> >
> >> Thank you. Just tried that, but I'm running into errors when it tries to
> >> create a table for an entity that is supposed to become a CHAR(1) field
> >> based on the annotations in the entity, but it tries to generate the
> field
> >> as CHAR(255), which it complains about. Here is a snippet of the entity
> and
> >> the log messages:
> >>
> >>> @Entity
> >>> @Table(name = "DISCOUNT_CODE")
> >>> @XmlRootElement
> >>> @NamedQueries({
> >>>    @NamedQuery(name = "DiscountCode.findAll", query = "SELECT d FROM
> >> DiscountCode d"),
> >>>    @NamedQuery(name = "DiscountCode.findByDiscountCode", query =
> >> "SELECT d FROM DiscountCode d WHERE d.discountCode = :discountCode"),
> >>>    @NamedQuery(name = "DiscountCode.findByRate", query = "SELECT d FROM
> >> DiscountCode d WHERE d.rate = :rate")})
> >>> public class DiscountCode implements Serializable {
> >>>    private static final long serialVersionUID = 1L;
> >>>    @Id
> >>>    @Basic(optional = false)
> >>>    @NotNull
> >>>    @Size(min = 1, max = 1)
> >>>    @Column(name = "DISCOUNT_CODE")
> >>>    private String discountCode;
> >>>    // @Max(value=?)  @Min(value=?)//if you know range of your decimal
> >> fields consider using these annotations to enforce field validation
> >>>    @Column(name = "RATE")
> >>>    private BigDecimal rate;
> >>>    @OneToMany(cascade = CascadeType.ALL, mappedBy = "discountCode",
> >> fetch = FetchType.EAGER)
> >>>    private Collection<Customer> customerCollection;
> >>
> >>
> >>> SEVERE: EjbTransactionUtil.handleSystemException: The length,
> precision,
> >> or scale attribute for column, or type mapping 'CHAR(255)' is not valid.
> >> {stmnt 971815908 CREATE TABLE PRODUCT_CODE -- ProductCode
> >>>    (PROD_CODE VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255),
> >> DISCOUNT_CODE CHAR(255), PRIMARY KEY (PROD_CODE))} [code=30000,
> state=42611]
> >>> <openjpa-2.4.0-nonfinal-1598334-r422266:1599166 nonfatal general error>
> >> org.apache.openjpa.persistence.PersistenceException: The length,
> precision,
> >> or scale attribute for column, or type mapping 'CHAR(255)' is not valid.
> >> {stmnt 971815908 CREATE TABLE PRODUCT_CODE -- ProductCode
> >>>    (PROD_CODE VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255),
> >> DISCOUNT_CODE CHAR(255), PRIMARY KEY (PROD_CODE))} [code=30000,
> state=42611]
> >>>      at
> >> org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:559)
> >>>      at
> >> org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:455)
> >>>
> >>
> >>
> >> Any idea why that is? I mean there's a @Size annotation in the entity
> that
> >> tell it the max size. Shouldn't the mapper tool take that into account?
> >>
> >> Kay
> >>
> >>> On Apr 24, 2015, at 10:59 AM, Jody Grassel <fy...@gmail.com> wrote:
> >>>
> >>> That directive will instruct OpenJPA to introspect the databases to
> >> ensure
> >>> the tables needed for the table schema defined by your ORM exists, and
> >> will
> >>> create the table structures automatically if they do not exist.  Do you
> >>> have a special need that requires OpenJPA to execute a provided SQL
> >> script?
> >>>
> >>> On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net>
> wrote:
> >>>
> >>>> Thanks, Jody. How will that let me provide an SQL script containing
> >> CREATE
> >>>> TABLE and INSERT statements?
> >>>>
> >>>> Kay
> >>>>
> >>>>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com>
> wrote:
> >>>>>
> >>>>> Add the following property to your persistence unit:
> >>>>>
> >>>>> <property name="openjpa.jdbc.SynchronizeMappings"
> >>>>> value="buildSchema(ForeignKeys=true)"/>
> >>>>>
> >>>>>
> >>>>>
> >>>>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net>
> >> wrote:
> >>>>>
> >>>>>> Hi everybody.
> >>>>>>
> >>>>>> I am having a rough time finding a way to initialize an Embedded
> >>>> In-Memory
> >>>>>> Derby database in my web application. I found a reference on
> Oracle's
> >>>> web
> >>>>>> site that states you can initialize a database with DDL and DML
> >>>> statements
> >>>>>> using properties like the following:
> >>>>>>
> >>>>>>>      <properties>
> >>>>>>>          <property
> >>>>>> name="javax.persistence.schema-generation.database.action"
> >>>>>> value="drop-and-create"/>
> >>>>>>>          <property
> >>>>>> name="javax.persistence.schema-generation.create-source"
> >>>> value="script"/>
> >>>>>>>          <property
> >>>>>> name="javax.persistence.schema-generation.create-script-source"
> >>>>>> value="META-INF/sql/create.sql" />
> >>>>>>>          <property name="javax.persistence.sql-load-script-source"
> >>>>>> value="META-INF/sql/data.sql" />
> >>>>>>>      </properties>
> >>>>>>
> >>>>>>
> >>>>>> However, that seems to be a new feature in JPA 2.1 spec as part of
> JEE
> >>>> 6.
> >>>>>> I am working with OpenJPA provided by Apache TomEE, which is
> >>>> openjpa-2.4.0
> >>>>>> non-final, a JPA 2.0 implementation I would imagine. The current
> >>>> release on
> >>>>>> OpenJPA web site is openjpa 2.3.
> >>>>>>
> >>>>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
> >>>> properties?
> >>>>>> I am trying to initialize an in-memory database for a test case I
> try
> >> to
> >>>>>> provide to someone.
> >>>>>>
> >>>>>> Any help would be much appreciated.
> >>>>>>
> >>>>>> Kay Wrobel
> >>>>
> >>>>
> >>
> >>
>
>

Re: Initialize Database Via Persistence.xml

Posted by Kay Wrobel <ka...@gmx.net>.
Thanks. I will try that.

I will still be completely stuck populating the database with pre-existing "entities", if you know what I mean. I know I could write a class that is application-scoped and use the @PostConstruct annotation to do some preliminary work, like creating entities using the entity manager. But I have like 60-70 records in that database in like 6 or 7 tables and no way to turn those database records into Java code that simulated the new Entity(x,x,x,x,x); nomenclature. And I really don't want to type all of that up. Makes sense?

Kay

> On Apr 24, 2015, at 11:16 AM, Jody Grassel <fy...@gmail.com> wrote:
> 
> @Size is a bean validation annotation, which is not going to govern
> schema.  Try adding length=1 to your @Column annotation for discountCode
> 
> @Id
> @Basic(optional = false)  // not really necessary as @Ids are always
> non-optional
> @NotNull
> @Size(min = 1, max = 1)
> @Column(name = "DISCOUNT_CODE"*, length=1*)
> private String discountCode;
> 
> On Fri, Apr 24, 2015 at 11:08 AM, Kay Wrobel <ka...@gmx.net> wrote:
> 
>> Thank you. Just tried that, but I'm running into errors when it tries to
>> create a table for an entity that is supposed to become a CHAR(1) field
>> based on the annotations in the entity, but it tries to generate the field
>> as CHAR(255), which it complains about. Here is a snippet of the entity and
>> the log messages:
>> 
>>> @Entity
>>> @Table(name = "DISCOUNT_CODE")
>>> @XmlRootElement
>>> @NamedQueries({
>>>    @NamedQuery(name = "DiscountCode.findAll", query = "SELECT d FROM
>> DiscountCode d"),
>>>    @NamedQuery(name = "DiscountCode.findByDiscountCode", query =
>> "SELECT d FROM DiscountCode d WHERE d.discountCode = :discountCode"),
>>>    @NamedQuery(name = "DiscountCode.findByRate", query = "SELECT d FROM
>> DiscountCode d WHERE d.rate = :rate")})
>>> public class DiscountCode implements Serializable {
>>>    private static final long serialVersionUID = 1L;
>>>    @Id
>>>    @Basic(optional = false)
>>>    @NotNull
>>>    @Size(min = 1, max = 1)
>>>    @Column(name = "DISCOUNT_CODE")
>>>    private String discountCode;
>>>    // @Max(value=?)  @Min(value=?)//if you know range of your decimal
>> fields consider using these annotations to enforce field validation
>>>    @Column(name = "RATE")
>>>    private BigDecimal rate;
>>>    @OneToMany(cascade = CascadeType.ALL, mappedBy = "discountCode",
>> fetch = FetchType.EAGER)
>>>    private Collection<Customer> customerCollection;
>> 
>> 
>>> SEVERE: EjbTransactionUtil.handleSystemException: The length, precision,
>> or scale attribute for column, or type mapping 'CHAR(255)' is not valid.
>> {stmnt 971815908 CREATE TABLE PRODUCT_CODE -- ProductCode
>>>    (PROD_CODE VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255),
>> DISCOUNT_CODE CHAR(255), PRIMARY KEY (PROD_CODE))} [code=30000, state=42611]
>>> <openjpa-2.4.0-nonfinal-1598334-r422266:1599166 nonfatal general error>
>> org.apache.openjpa.persistence.PersistenceException: The length, precision,
>> or scale attribute for column, or type mapping 'CHAR(255)' is not valid.
>> {stmnt 971815908 CREATE TABLE PRODUCT_CODE -- ProductCode
>>>    (PROD_CODE VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255),
>> DISCOUNT_CODE CHAR(255), PRIMARY KEY (PROD_CODE))} [code=30000, state=42611]
>>>      at
>> org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:559)
>>>      at
>> org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:455)
>>> 
>> 
>> 
>> Any idea why that is? I mean there's a @Size annotation in the entity that
>> tell it the max size. Shouldn't the mapper tool take that into account?
>> 
>> Kay
>> 
>>> On Apr 24, 2015, at 10:59 AM, Jody Grassel <fy...@gmail.com> wrote:
>>> 
>>> That directive will instruct OpenJPA to introspect the databases to
>> ensure
>>> the tables needed for the table schema defined by your ORM exists, and
>> will
>>> create the table structures automatically if they do not exist.  Do you
>>> have a special need that requires OpenJPA to execute a provided SQL
>> script?
>>> 
>>> On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net> wrote:
>>> 
>>>> Thanks, Jody. How will that let me provide an SQL script containing
>> CREATE
>>>> TABLE and INSERT statements?
>>>> 
>>>> Kay
>>>> 
>>>>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com> wrote:
>>>>> 
>>>>> Add the following property to your persistence unit:
>>>>> 
>>>>> <property name="openjpa.jdbc.SynchronizeMappings"
>>>>> value="buildSchema(ForeignKeys=true)"/>
>>>>> 
>>>>> 
>>>>> 
>>>>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net>
>> wrote:
>>>>> 
>>>>>> Hi everybody.
>>>>>> 
>>>>>> I am having a rough time finding a way to initialize an Embedded
>>>> In-Memory
>>>>>> Derby database in my web application. I found a reference on Oracle's
>>>> web
>>>>>> site that states you can initialize a database with DDL and DML
>>>> statements
>>>>>> using properties like the following:
>>>>>> 
>>>>>>>      <properties>
>>>>>>>          <property
>>>>>> name="javax.persistence.schema-generation.database.action"
>>>>>> value="drop-and-create"/>
>>>>>>>          <property
>>>>>> name="javax.persistence.schema-generation.create-source"
>>>> value="script"/>
>>>>>>>          <property
>>>>>> name="javax.persistence.schema-generation.create-script-source"
>>>>>> value="META-INF/sql/create.sql" />
>>>>>>>          <property name="javax.persistence.sql-load-script-source"
>>>>>> value="META-INF/sql/data.sql" />
>>>>>>>      </properties>
>>>>>> 
>>>>>> 
>>>>>> However, that seems to be a new feature in JPA 2.1 spec as part of JEE
>>>> 6.
>>>>>> I am working with OpenJPA provided by Apache TomEE, which is
>>>> openjpa-2.4.0
>>>>>> non-final, a JPA 2.0 implementation I would imagine. The current
>>>> release on
>>>>>> OpenJPA web site is openjpa 2.3.
>>>>>> 
>>>>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
>>>> properties?
>>>>>> I am trying to initialize an in-memory database for a test case I try
>> to
>>>>>> provide to someone.
>>>>>> 
>>>>>> Any help would be much appreciated.
>>>>>> 
>>>>>> Kay Wrobel
>>>> 
>>>> 
>> 
>> 


Re: Initialize Database Via Persistence.xml

Posted by Jody Grassel <fy...@gmail.com>.
@Size is a bean validation annotation, which is not going to govern
schema.  Try adding length=1 to your @Column annotation for discountCode

@Id
@Basic(optional = false)  // not really necessary as @Ids are always
non-optional
@NotNull
@Size(min = 1, max = 1)
@Column(name = "DISCOUNT_CODE"*, length=1*)
private String discountCode;

On Fri, Apr 24, 2015 at 11:08 AM, Kay Wrobel <ka...@gmx.net> wrote:

> Thank you. Just tried that, but I'm running into errors when it tries to
> create a table for an entity that is supposed to become a CHAR(1) field
> based on the annotations in the entity, but it tries to generate the field
> as CHAR(255), which it complains about. Here is a snippet of the entity and
> the log messages:
>
> > @Entity
> > @Table(name = "DISCOUNT_CODE")
> > @XmlRootElement
> > @NamedQueries({
> >     @NamedQuery(name = "DiscountCode.findAll", query = "SELECT d FROM
> DiscountCode d"),
> >     @NamedQuery(name = "DiscountCode.findByDiscountCode", query =
> "SELECT d FROM DiscountCode d WHERE d.discountCode = :discountCode"),
> >     @NamedQuery(name = "DiscountCode.findByRate", query = "SELECT d FROM
> DiscountCode d WHERE d.rate = :rate")})
> > public class DiscountCode implements Serializable {
> >     private static final long serialVersionUID = 1L;
> >     @Id
> >     @Basic(optional = false)
> >     @NotNull
> >     @Size(min = 1, max = 1)
> >     @Column(name = "DISCOUNT_CODE")
> >     private String discountCode;
> >     // @Max(value=?)  @Min(value=?)//if you know range of your decimal
> fields consider using these annotations to enforce field validation
> >     @Column(name = "RATE")
> >     private BigDecimal rate;
> >     @OneToMany(cascade = CascadeType.ALL, mappedBy = "discountCode",
> fetch = FetchType.EAGER)
> >     private Collection<Customer> customerCollection;
>
>
> > SEVERE: EjbTransactionUtil.handleSystemException: The length, precision,
> or scale attribute for column, or type mapping 'CHAR(255)' is not valid.
> {stmnt 971815908 CREATE TABLE PRODUCT_CODE -- ProductCode
> >     (PROD_CODE VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255),
> DISCOUNT_CODE CHAR(255), PRIMARY KEY (PROD_CODE))} [code=30000, state=42611]
> > <openjpa-2.4.0-nonfinal-1598334-r422266:1599166 nonfatal general error>
> org.apache.openjpa.persistence.PersistenceException: The length, precision,
> or scale attribute for column, or type mapping 'CHAR(255)' is not valid.
> {stmnt 971815908 CREATE TABLE PRODUCT_CODE -- ProductCode
> >     (PROD_CODE VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255),
> DISCOUNT_CODE CHAR(255), PRIMARY KEY (PROD_CODE))} [code=30000, state=42611]
> >       at
> org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:559)
> >       at
> org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:455)
> >
>
>
> Any idea why that is? I mean there's a @Size annotation in the entity that
> tell it the max size. Shouldn't the mapper tool take that into account?
>
> Kay
>
> > On Apr 24, 2015, at 10:59 AM, Jody Grassel <fy...@gmail.com> wrote:
> >
> > That directive will instruct OpenJPA to introspect the databases to
> ensure
> > the tables needed for the table schema defined by your ORM exists, and
> will
> > create the table structures automatically if they do not exist.  Do you
> > have a special need that requires OpenJPA to execute a provided SQL
> script?
> >
> > On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net> wrote:
> >
> >> Thanks, Jody. How will that let me provide an SQL script containing
> CREATE
> >> TABLE and INSERT statements?
> >>
> >> Kay
> >>
> >>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com> wrote:
> >>>
> >>> Add the following property to your persistence unit:
> >>>
> >>> <property name="openjpa.jdbc.SynchronizeMappings"
> >>> value="buildSchema(ForeignKeys=true)"/>
> >>>
> >>>
> >>>
> >>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net>
> wrote:
> >>>
> >>>> Hi everybody.
> >>>>
> >>>> I am having a rough time finding a way to initialize an Embedded
> >> In-Memory
> >>>> Derby database in my web application. I found a reference on Oracle's
> >> web
> >>>> site that states you can initialize a database with DDL and DML
> >> statements
> >>>> using properties like the following:
> >>>>
> >>>>>       <properties>
> >>>>>           <property
> >>>> name="javax.persistence.schema-generation.database.action"
> >>>> value="drop-and-create"/>
> >>>>>           <property
> >>>> name="javax.persistence.schema-generation.create-source"
> >> value="script"/>
> >>>>>           <property
> >>>> name="javax.persistence.schema-generation.create-script-source"
> >>>> value="META-INF/sql/create.sql" />
> >>>>>           <property name="javax.persistence.sql-load-script-source"
> >>>> value="META-INF/sql/data.sql" />
> >>>>>       </properties>
> >>>>
> >>>>
> >>>> However, that seems to be a new feature in JPA 2.1 spec as part of JEE
> >> 6.
> >>>> I am working with OpenJPA provided by Apache TomEE, which is
> >> openjpa-2.4.0
> >>>> non-final, a JPA 2.0 implementation I would imagine. The current
> >> release on
> >>>> OpenJPA web site is openjpa 2.3.
> >>>>
> >>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
> >> properties?
> >>>> I am trying to initialize an in-memory database for a test case I try
> to
> >>>> provide to someone.
> >>>>
> >>>> Any help would be much appreciated.
> >>>>
> >>>> Kay Wrobel
> >>
> >>
>
>

Re: Initialize Database Via Persistence.xml

Posted by Kay Wrobel <ka...@gmx.net>.
Thank you. Just tried that, but I'm running into errors when it tries to create a table for an entity that is supposed to become a CHAR(1) field based on the annotations in the entity, but it tries to generate the field as CHAR(255), which it complains about. Here is a snippet of the entity and the log messages:

> @Entity
> @Table(name = "DISCOUNT_CODE")
> @XmlRootElement
> @NamedQueries({
>     @NamedQuery(name = "DiscountCode.findAll", query = "SELECT d FROM DiscountCode d"),
>     @NamedQuery(name = "DiscountCode.findByDiscountCode", query = "SELECT d FROM DiscountCode d WHERE d.discountCode = :discountCode"),
>     @NamedQuery(name = "DiscountCode.findByRate", query = "SELECT d FROM DiscountCode d WHERE d.rate = :rate")})
> public class DiscountCode implements Serializable {
>     private static final long serialVersionUID = 1L;
>     @Id
>     @Basic(optional = false)
>     @NotNull
>     @Size(min = 1, max = 1)
>     @Column(name = "DISCOUNT_CODE")
>     private String discountCode;
>     // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
>     @Column(name = "RATE")
>     private BigDecimal rate;
>     @OneToMany(cascade = CascadeType.ALL, mappedBy = "discountCode", fetch = FetchType.EAGER)
>     private Collection<Customer> customerCollection;


> SEVERE: EjbTransactionUtil.handleSystemException: The length, precision, or scale attribute for column, or type mapping 'CHAR(255)' is not valid.  {stmnt 971815908 CREATE TABLE PRODUCT_CODE -- ProductCode
>     (PROD_CODE VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255), DISCOUNT_CODE CHAR(255), PRIMARY KEY (PROD_CODE))} [code=30000, state=42611]
> <openjpa-2.4.0-nonfinal-1598334-r422266:1599166 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: The length, precision, or scale attribute for column, or type mapping 'CHAR(255)' is not valid.  {stmnt 971815908 CREATE TABLE PRODUCT_CODE -- ProductCode
>     (PROD_CODE VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255), DISCOUNT_CODE CHAR(255), PRIMARY KEY (PROD_CODE))} [code=30000, state=42611]
> 	at org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:559)
> 	at org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:455)
> 


Any idea why that is? I mean there's a @Size annotation in the entity that tell it the max size. Shouldn't the mapper tool take that into account?

Kay

> On Apr 24, 2015, at 10:59 AM, Jody Grassel <fy...@gmail.com> wrote:
> 
> That directive will instruct OpenJPA to introspect the databases to ensure
> the tables needed for the table schema defined by your ORM exists, and will
> create the table structures automatically if they do not exist.  Do you
> have a special need that requires OpenJPA to execute a provided SQL script?
> 
> On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net> wrote:
> 
>> Thanks, Jody. How will that let me provide an SQL script containing CREATE
>> TABLE and INSERT statements?
>> 
>> Kay
>> 
>>> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com> wrote:
>>> 
>>> Add the following property to your persistence unit:
>>> 
>>> <property name="openjpa.jdbc.SynchronizeMappings"
>>> value="buildSchema(ForeignKeys=true)"/>
>>> 
>>> 
>>> 
>>> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net> wrote:
>>> 
>>>> Hi everybody.
>>>> 
>>>> I am having a rough time finding a way to initialize an Embedded
>> In-Memory
>>>> Derby database in my web application. I found a reference on Oracle's
>> web
>>>> site that states you can initialize a database with DDL and DML
>> statements
>>>> using properties like the following:
>>>> 
>>>>>       <properties>
>>>>>           <property
>>>> name="javax.persistence.schema-generation.database.action"
>>>> value="drop-and-create"/>
>>>>>           <property
>>>> name="javax.persistence.schema-generation.create-source"
>> value="script"/>
>>>>>           <property
>>>> name="javax.persistence.schema-generation.create-script-source"
>>>> value="META-INF/sql/create.sql" />
>>>>>           <property name="javax.persistence.sql-load-script-source"
>>>> value="META-INF/sql/data.sql" />
>>>>>       </properties>
>>>> 
>>>> 
>>>> However, that seems to be a new feature in JPA 2.1 spec as part of JEE
>> 6.
>>>> I am working with OpenJPA provided by Apache TomEE, which is
>> openjpa-2.4.0
>>>> non-final, a JPA 2.0 implementation I would imagine. The current
>> release on
>>>> OpenJPA web site is openjpa 2.3.
>>>> 
>>>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
>> properties?
>>>> I am trying to initialize an in-memory database for a test case I try to
>>>> provide to someone.
>>>> 
>>>> Any help would be much appreciated.
>>>> 
>>>> Kay Wrobel
>> 
>> 


Re: Initialize Database Via Persistence.xml

Posted by Jody Grassel <fy...@gmail.com>.
That directive will instruct OpenJPA to introspect the databases to ensure
the tables needed for the table schema defined by your ORM exists, and will
create the table structures automatically if they do not exist.  Do you
have a special need that requires OpenJPA to execute a provided SQL script?

On Fri, Apr 24, 2015 at 10:54 AM, Kay Wrobel <ka...@gmx.net> wrote:

> Thanks, Jody. How will that let me provide an SQL script containing CREATE
> TABLE and INSERT statements?
>
> Kay
>
> > On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com> wrote:
> >
> > Add the following property to your persistence unit:
> >
> > <property name="openjpa.jdbc.SynchronizeMappings"
> > value="buildSchema(ForeignKeys=true)"/>
> >
> >
> >
> > On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net> wrote:
> >
> >> Hi everybody.
> >>
> >> I am having a rough time finding a way to initialize an Embedded
> In-Memory
> >> Derby database in my web application. I found a reference on Oracle's
> web
> >> site that states you can initialize a database with DDL and DML
> statements
> >> using properties like the following:
> >>
> >>>        <properties>
> >>>            <property
> >> name="javax.persistence.schema-generation.database.action"
> >> value="drop-and-create"/>
> >>>            <property
> >> name="javax.persistence.schema-generation.create-source"
> value="script"/>
> >>>            <property
> >> name="javax.persistence.schema-generation.create-script-source"
> >> value="META-INF/sql/create.sql" />
> >>>            <property name="javax.persistence.sql-load-script-source"
> >> value="META-INF/sql/data.sql" />
> >>>        </properties>
> >>
> >>
> >> However, that seems to be a new feature in JPA 2.1 spec as part of JEE
> 6.
> >> I am working with OpenJPA provided by Apache TomEE, which is
> openjpa-2.4.0
> >> non-final, a JPA 2.0 implementation I would imagine. The current
> release on
> >> OpenJPA web site is openjpa 2.3.
> >>
> >> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA
> properties?
> >> I am trying to initialize an in-memory database for a test case I try to
> >> provide to someone.
> >>
> >> Any help would be much appreciated.
> >>
> >> Kay Wrobel
>
>

Re: Initialize Database Via Persistence.xml

Posted by Kay Wrobel <ka...@gmx.net>.
Thanks, Jody. How will that let me provide an SQL script containing CREATE TABLE and INSERT statements?

Kay

> On Apr 24, 2015, at 10:51 AM, Jody Grassel <fy...@gmail.com> wrote:
> 
> Add the following property to your persistence unit:
> 
> <property name="openjpa.jdbc.SynchronizeMappings"
> value="buildSchema(ForeignKeys=true)"/>
> 
> 
> 
> On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net> wrote:
> 
>> Hi everybody.
>> 
>> I am having a rough time finding a way to initialize an Embedded In-Memory
>> Derby database in my web application. I found a reference on Oracle's web
>> site that states you can initialize a database with DDL and DML statements
>> using properties like the following:
>> 
>>>        <properties>
>>>            <property
>> name="javax.persistence.schema-generation.database.action"
>> value="drop-and-create"/>
>>>            <property
>> name="javax.persistence.schema-generation.create-source" value="script"/>
>>>            <property
>> name="javax.persistence.schema-generation.create-script-source"
>> value="META-INF/sql/create.sql" />
>>>            <property name="javax.persistence.sql-load-script-source"
>> value="META-INF/sql/data.sql" />
>>>        </properties>
>> 
>> 
>> However, that seems to be a new feature in JPA 2.1 spec as part of JEE 6.
>> I am working with OpenJPA provided by Apache TomEE, which is openjpa-2.4.0
>> non-final, a JPA 2.0 implementation I would imagine. The current release on
>> OpenJPA web site is openjpa 2.3.
>> 
>> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA properties?
>> I am trying to initialize an in-memory database for a test case I try to
>> provide to someone.
>> 
>> Any help would be much appreciated.
>> 
>> Kay Wrobel


Re: Initialize Database Via Persistence.xml

Posted by Jody Grassel <fy...@gmail.com>.
Add the following property to your persistence unit:

<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)"/>



On Fri, Apr 24, 2015 at 10:47 AM, Kay Wrobel <ka...@gmx.net> wrote:

> Hi everybody.
>
> I am having a rough time finding a way to initialize an Embedded In-Memory
> Derby database in my web application. I found a reference on Oracle's web
> site that states you can initialize a database with DDL and DML statements
> using properties like the following:
>
> >         <properties>
> >             <property
> name="javax.persistence.schema-generation.database.action"
> value="drop-and-create"/>
> >             <property
> name="javax.persistence.schema-generation.create-source" value="script"/>
> >             <property
> name="javax.persistence.schema-generation.create-script-source"
> value="META-INF/sql/create.sql" />
> >             <property name="javax.persistence.sql-load-script-source"
> value="META-INF/sql/data.sql" />
> >         </properties>
>
>
> However, that seems to be a new feature in JPA 2.1 spec as part of JEE 6.
> I am working with OpenJPA provided by Apache TomEE, which is openjpa-2.4.0
> non-final, a JPA 2.0 implementation I would imagine. The current release on
> OpenJPA web site is openjpa 2.3.
>
> Is there a way to accomplish this via JPA 2.0  and/or OpenJPA properties?
> I am trying to initialize an in-memory database for a test case I try to
> provide to someone.
>
> Any help would be much appreciated.
>
> Kay Wrobel