JPA persistence.xml I want it to connect to MariaD

2019-07-26 20:01发布

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>

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

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)

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

Additional information: I am using TomEE 8.0.0 M1

1条回答
冷血范
2楼-- · 2019-07-26 20:20

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 Resource configuration: http://tomee.apache.org/datasource-config.html

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

查看更多
登录 后发表回答