0

I'm trying to use pure JPA 2.1 as a standardized way to generate db schema. (Underlying database - Derby embedded, persistence provider - eclipseLink)

To check generated scripts I set <property name="javax.persistence.schema-generation-target" value="database-and-scripts"/> And as a result I got it with some 'sequence' table within. And although ... ERROR 42X05: Table/View 'SEQUENCE' does not exist.

Can somebody help to understand such a weird behavior?

maven dependencies:

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa</artifactId>
    <version>2.6.0</version>
</dependency>
<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>10.9.1.0</version>
</dependency>

Entity:

import javax.persistence.*;

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String title;
// getters/setters/constructors
}

main body:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("chapter04PU");
EntityManager em = emf.createEntityManager();

EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(new Book("Neuromancer"));
tx.commit();

em.close();
emf.close();

persistence-unit:

<persistence-unit name="chapter04PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

    <class>Book</class>
    <properties>
        <!--<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>-->
        <property name="javax.persistence.schema-generation-action" value="drop-and-create"/>
        <property name="javax.persistence.schema-generation-target" value="database-and-scripts"/>
    <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
    <property name="javax.persistence.schema-generation.scripts.create-target" value="create.sql"/>
    <property name="javax.persistence.schema-generation.scripts.drop-target" value="delete.sql"/>

    <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
    <property name="javax.persistence.jdbc.url" value="jdbc:derby:chapter04DB;create=true"/>
</properties>

generated script file:

CREATE TABLE BOOK (ID BIGINT NOT NULL, TITLE VARCHAR(255), PRIMARY KEY (ID))
CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(15), PRIMARY KEY (SEQ_NAME))
INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 0)

Update After replacing:

<property name="javax.persistence.schema-generation-action" value="drop-and-create"/>

with:

<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>

I can 'normally' run app (with exit code 0).

But I still take this weird message while first run (with absent DB).

enter image description here the second run is clear - no such messages.

JimHawkins
  • 4,843
  • 8
  • 35
  • 55
Alex Silkovsky
  • 541
  • 5
  • 18

1 Answers1

0

Thanks a lot for your suggestion to use <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>!

As for the second problem, the main culprit here is @GeneratedValue(strategy=GenerationType.AUTO) which probably was a default value provided by IDE on entity creation. Given the way org.eclipse.persistence.jpa.PersistenceProvider processes this annotation (leading to the exception about non-existant 'SEQUENCE' table), the simplest solution would be to just use @GeneratedValue(strategy=GenerationType.IDENTITY) instead.

Wade
  • 418
  • 4
  • 9