2

我有一个java webapp,

persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence 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_2.xsd"
         version="2.2">

<persistence-unit name="my-persistence-unit">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>

        <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/qltb"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="my_passwowrd"/>

        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MariaDB103Dialect"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
        <property name="hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>

我创建了一个简单的Bottle @Entity类,它只包含一个 id 和一个 String 属性。

当我运行这个 webapp 时,我在日志中看到它正在处理my-persistence-unit但抛出了一个异常:

org.hibernate.jpa.internal.util.LogHelper.logPersistenceUnitInformation HHH000204: Processing PersistenceUnitInfo [
name: my-persistence-unit
...]

... // Some lines are skipped


org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
create table Bottle (
   id bigint not null,
    color varchar(255),
    primary key (id)
) engine=InnoDB" via JDBC Statement

.... // Some lines are skipped
Caused by: org.hsqldb.HsqlException: unexpected token: ENGINE : line: 6
at org.hsqldb.error.Error.parseError(Unknown Source)
at org.hsqldb.ParserBase.unexpectedToken(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
at org.hsqldb.Session.executeDirectStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)

为什么 JPA 连接到 hsqldb 而不是 persistence.xml 中声明的 MariaDB?

附加信息:我使用的是 TomEE 8.0.0 M1

4

1 回答 1

1

我用以下内容添加resources.xmlWEB-INF(而不是)中,现在它可以工作了:META-INF/context.xml

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
    <Resource id="myDataSource" type="javax.sql.DataSource">
        jdbcDriver=org.mariadb.jdbc.Driver
        jdbcUrl = jdbc:mariadb://localhost:3306/qltb
        userName = root
        password = password
        maxActive = 20
</Resource>

TomEE 资源配置:http ://tomee.apache.org/datasource-config.html

我仍然不知道persistence.xml和之间的关系resources.xml记录在哪里(在这种情况下)。我什至不需要<jta-data-source>标签persistence.xml

于 2018-12-04T16:17:06.367 回答