JPA persistence.xml我希望它连接到MariaDB,但始终连接到hsqldb [英] JPA persistence.xml I want it to connect to MariaDB but it always connects to hsqldb

查看:61
本文介绍了JPA persistence.xml我希望它连接到MariaDB,但始终连接到hsqldb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java Webapp,

I have a 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属性.

I create a simple Bottle @Entity class, which contains only an id and a String property.

当我运行此Web应用程序时,在日志中看到它正在处理 my-persistence-unit ,但引发了异常:

When I run this webapp, I see in the log that it was processing my-persistence-unit but an exception was thrown:

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?

Why JPA is connecting to hsqldb instead of MariaDB as declared in persistence.xml?

其他信息:我正在使用TomEE 8.0.0 M1

Additional information: I am using TomEE 8.0.0 M1

推荐答案

我将 resources.xml 添加到 WEB-INF 中(而不是 META-INF/context.xml )包含以下内容,现在可以使用:

I added resources.xml into WEB-INF (and NOT META-INF/context.xml) with following content and now it works:

<?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

TomEE Resource configuration: http://tomee.apache.org/datasource-config.html

我仍然不知道 persistence.xml resources.xml 之间的关系在哪里记录(在这种情况下).我什至不需要 persistence.xml

I still don't know where the relationship between persistence.xml and resources.xml is documented (in this case). I don't even need a <jta-data-source> tag in persistence.xml

这篇关于JPA persistence.xml我希望它连接到MariaDB,但始终连接到hsqldb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆