DerbyDB GlassFish简单示例SEQUENCE不存在

时间:2015-01-18 13:33:05

标签: derby glassfish-4

我正在尝试从书籍Beginning_Java_EE_6_with_GlassFish_3(DerbyDB + eclipselink + maven)中运行简单的JPA示例。 问题与问题相同 The same question

但我的持久性配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
<persistence 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_2_0.xsd"
             version="2.0">
    <persistence-unit name="chapter02PU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/__default</jta-data-source>
        <class>com.apress.javaee6.chapter02.Book</class>
        <properties>
            <property name="elipselink.target-database" value="DERBY"/>
            <property name="elipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="elipselink.logging.level" value="ALL"/>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/chapter02DB;create=true"/>
            <property name="javax.persistence.jdbc.user" value="APP"/>
            <property name="javax.persistence.jdbc.password" value="APP"/>
        </properties>
    </persistence-unit>
</persistence>

这是主要类:

package com.apress.javaee6.chapter02;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class Main {

    public static void main(String[] args) {
        Book book = new Book.BookBuilder()
                .title("The Chitchiker's description to Galaxy")
                .price(12.5F)
                .description("Since fiction comedy book")
                .isbn("1-84023-666")
                .numOfPage(354)
                .illustration(false)
                .build();

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("chapter02PU");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();

        transaction.begin();
        entityManager.persist(book);
        transaction.commit();

        entityManager.close();
        entityManagerFactory.close();
    }
}

Book.class

@Entity
@NamedQuery(name = "findAllBooks", query = "select b from Book b")
public class Book {

    public Book() {
    }

    @Id @GeneratedValue
    private Long id;

    @Column(nullable = false)
    private String title;

    private Float price;

    @Column(length = 2000)
    private String description;

    private String isbn;

    private Integer numOfPage;

    private Boolean illustration;

所以我也查了几个关于Glassfish和JDBC Resource的问题(how to setup JTA datasource),但它们出现在GlassFish控制台(jdbc / __ default)中。

最后,当我用命令

运行我的程序时
mvn exec:java -Dexec.mainClass="com.apress.javaee6.chapter02.Main"

我的pom.xml是

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.apress.javaee6</groupId>
    <artifactId>chapter02</artifactId>
    <version>1.0</version>
    <name>chapter02</name>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbyclient</artifactId>
            <version>10.10.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.10.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我收到错误

[ERROR] Internal Exception: java.sql.SQLSyntaxErrorException: Table/View 'SEQUEN
CE' does not exist.
[ERROR] Error Code: 20000
[ERROR] Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
[ERROR] bind => [2 parameters bound]
[ERROR] Query: DataModifyQuery(name="SEQUENCE" sql="UPDATE SEQUENCE SET SEQ_COUN
T = SEQ_COUNT + ? WHERE SEQ_NAME = ?")
[ERROR] -> [Help 1]
[ERROR]

我搜索相关问题,但我不知道我的错误在哪里。有人能帮我吗? 的 UPD1 我在derbyDB中启用了日志:

Database Class Loader started - derby.database.classpath=''
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 166), (SESSIONID = 0), (DATABASE = chapter02DB), (DRDAID = {1}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 166), (SESSIONID = 0), (DATABASE = chapter02DB), (DRDAID = {1}), Rolling back
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 167), (SESSIONID = 1), (DATABASE = chapter02DB), (DRDAID = {1}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 167), (SESSIONID = 1), (DATABASE = chapter02DB), (DRDAID = {1}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 167), (SESSIONID = 1), (DATABASE = chapter02DB), (DRDAID = ????????.????-4255337276300706295{1}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 167), (SESSIONID = 1), (DATABASE = chapter02DB), (DRDAID = ????????.????-4255337276300706295{1}), Rolling back
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 167), (SESSIONID = 1), (DATABASE = chapter02DB), (DRDAID = ????????.????-4255337276300706295{1}), Rolling back
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 168), (SESSIONID = 2), (DATABASE = chapter02DB), (DRDAID = {2}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 168), (SESSIONID = 2), (DATABASE = chapter02DB), (DRDAID = {2}), Rolling back
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 169), (SESSIONID = 3), (DATABASE = chapter02DB), (DRDAID = {2}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 169), (SESSIONID = 3), (DATABASE = chapter02DB), (DRDAID = {2}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 170), (SESSIONID = 3), (DATABASE = chapter02DB), (DRDAID = ????????.????-4254774326347276936{2}), Begin compiling prepared statement: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ? :End prepared statement
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 170), (SESSIONID = 3), (DATABASE = chapter02DB), (DRDAID = ????????.????-4254774326347276936{2}), Error compiling prepared statement: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ? :End prepared statement

1 个答案:

答案 0 :(得分:0)

这是persistance.xml中的错误

而不是

<properties>
    <property name="elipselink.target-database" value="DERBY"/>
    <property name="elipselink.ddl-generation" value="drop-and-create-tables"/>
    <property name="elipselink.logging.level" value="ALL"/>

应该是

<properties>
    <property name="eclipselink.target-database" value="DERBY"/>
    <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    <property name="eclipselink.logging.level" value="ALL"/>

启用derby DB中的事务日志真的对我有帮助(Dderby.language.logStatementText = true)。从这些日志中我看到在启动时没有创建表