无法在Java EE中用MySQL和TomEE配置Hibernate(方言错误?)>

问题描述 投票:0回答:1

[我正在学习Java EE,并且已经有2天的时间我一直在努力配置Hibernate,以便在简单的Java EE Web应用程序中的TomEE服务器上使用MySQL数据库。

  • Hibernate版本:5.4.10.Final(核心和实体管理器依赖性)
  • Java EE API:8.0
  • MySQL版本:8.0.19
  • TomEE版本:8.0.1(TomEE嵌入在tomee-embedded-maven-plugin中)
  • 我有2个简单的实体:汽车和座椅,从汽车到座椅具有单向@OneToMany关系。 Color和EngineType是普通枚举,而Specification是这两个枚举的值对象)。注意:@OneToMany中将FetchType.EAGER用于学习目的,我知道这通常不是一个好的解决方案。

[当我尝试配置persistence.xml文件时,alghouth指定了所有“针对MySQL”,看来Hibernate仍然使用默认的HSQLDB语法/方言/引擎

,因此,我收到错误在模式创建期间
[INFO] TomEE embedded started on localhost:8080
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
Hibernate: alter table Seat drop foreign key FKkkm9pdx9e1t9jva76n9tqhhqv
mar 06, 2020 1:48:31 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
WARN: GenerationTarget encountered exception accepting command : Error executing DDL "alter table Seat drop foreign key FKkkm9pdx9e1t9jva76n9tqhhqv" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table Seat drop foreign key FKkkm9pdx9e1t9jva76n9tqhhqv" via JDBC Statement
(...)
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: PUBLIC.SEAT
(...)
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: PUBLIC.SEAT     // what is going on here???

以及稍后创建DDL时使用的Hibernate SQL命令,我还:

Hibernate: create table Car (identifier bigint not null auto_increment, color varchar(255), engineType varchar(255), primary key (identifier)) engine=InnoDB
mar 06, 2020 1:48:31 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
WARN: GenerationTarget encountered exception accepting command : Error executing DDL "create table Car (identifier bigint not null auto_increment, color varchar(255), engineType varchar(255), primary key (identifier)) engine=InnoDB" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table Car (identifier bigint not null auto_increment, color varchar(255), engineType varchar(255), primary key (identifier)) engine=InnoDB" via JDBC Statement
(...)
Caused by: java.sql.SQLSyntaxErrorException: unexpected token: AUTO_INCREMENT
(...)
Caused by: org.hsqldb.HsqlException: unexpected token: AUTO_INCREMENT   // definitely something messed up, this is a correct MySQL token

当我省略所有“ MySQL内容”(驱动程序和方言),并使Hibernate使用默认的HSQLDB时,它工作正常……HSQLDB的DDL创建良好。

[我尝试了许多不同的配置和替代方法,通过Web和SO对其进行了搜索,但是没有发现Hibernate为何仍使用非MySQL语法的任何提示(但我不是100%确信这是问题的直接原因)。] >

[我试图在webapp下的resources.xml文件中指定数据源,但是它没有任何改变。

另外,我检查了HSQLDB是否可以从Maven构建中排除,但它附带了hibernate-core,因此这不是容易实现的,也许也无济于事。

我以前在Spring和SpringBoot中使用Hibernate,但是在Java EE中我完全迷失了,这就是为什么我问这个问题。

任何人都可以帮助解决此问题,并就所有这些的正确配置提供一些建议吗?

我在resources / META-INF下的persistence.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.2"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
    <persistence-unit name="my-persistence-unit" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:openejb/Resource/myJtaDatabase</jta-data-source>
        <!-- Entity classes -->
        <class>com.example.javaeecourse.entity.Car</class>
        <class>com.example.javaeecourse.entity.Seat</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/javaeecourse?serverTimezone=UTC" />
            <property name="hibernate.connection.username" value="root" />
            <property name="hibernate.connection.password" value="admin" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="tomee.jpa.factory.lazy" value="true" />
            <!--<property name="tomee.jpa.cdi=false" value="false" />-->
        </properties>
    </persistence-unit>
</persistence>

汽车实体:

package com.example.javaeecourse.entity;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

import static com.example.javaeecourse.entity.Car.FIND_ALL;

@Getter
@Setter
@Entity
@Table(name = "cars")
@NamedQuery(name = FIND_ALL, query = "SELECT car FROM Car car")
@NoArgsConstructor
public class Car {

    public static final String FIND_ALL = "Car.findAll";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long identifier;

    @Enumerated(EnumType.STRING)
    private Color color;
    @Enumerated(EnumType.STRING)
    private EngineType engineType;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "car", nullable = false)
    private Set<Seat> seats = new HashSet<>();
}

座位实体:

package com.example.javaeecourse.entity;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;

@Entity
@Table(name = "seats")
@NoArgsConstructor
@Getter
@Setter
public class Seat {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String seatMaterial;

    public Seat(String seatMaterial) {
        this.seatMaterial = seatMaterial;
    }
}

CarManufacturer类(@Stateless EJB,在其中调用EntityManager):

package com.example.javaeecourse.boundary;

import com.example.javaeecourse.control.CarFactory;
import com.example.javaeecourse.entity.Car;
import com.example.javaeecourse.entity.CarCreatedEvent;
import com.example.javaeecourse.entity.Specification;

import javax.ejb.Stateless;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Stateless
public class CarManufacturer {

    @Inject
    CarFactory carFactory;

    @Inject
    Event<CarCreatedEvent> carCreatedEvent;

    @PersistenceContext
    EntityManager entityManager;

    public Car manufactureCar(Specification specification) {
        Car car = carFactory.createCar(specification);
        entityManager.persist(car);
        carCreatedEvent.fire(new CarCreatedEvent(car.getIdentifier()));
        return car;
    }

    public List<Car> retrieveCars() {
        return entityManager.createNamedQuery(Car.FIND_ALL, Car.class).getResultList();
    }
}

CarFactory类,实例化实体:

package com.example.javaeecourse.control;

import com.example.javaeecourse.entity.*;

import javax.inject.Inject;

public class CarFactory {

    @Inject
    @DefaultCarColor
    Color randomCarColor;

    @Inject
    @DefaultCarEngineType
    EngineType randomCarEngineType;

    public Car createCar(Specification specification) {
        Car car = new Car();
        car.setColor(specification.getColor() == null ? randomCarColor : specification.getColor());
        car.setEngineType(specification.getEngineType() == null ? randomCarEngineType : specification.getEngineType());
        Seat seat = new Seat("Leather");
        car.getSeats().add(seat);
        return car;
    }
}

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.example</groupId>
  <artifactId>javaeecourse</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>Java EE Course App</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <failOnMissingWebXml>false</failOnMissingWebXml>
  </properties>

  <dependencies>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>8.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
      <version>8.0.19</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.4.10.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>5.4.10.Final</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.jaxrs</groupId>
      <artifactId>jackson-jaxrs-json-provider</artifactId>
      <version>2.9.0</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>javaeecourse</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <groupId>org.apache.tomee.maven</groupId>
          <artifactId>tomee-embedded-maven-plugin</artifactId>
          <version>8.0.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

我将不胜感激。

我正在学习Java EE,并且已经有2天的时间我一直在努力配置Hibernate,以便在简单的Java EE Web应用程序中的TomEE服务器上使用MySQL数据库。休眠版本:...

mysql hibernate jpa tomee java-ee-8
1个回答
1
投票

感谢问题评论中@areus提供的提示,我得以解决此案。如果将来有人遇到类似问题,我会在此处发布答案。

有1个主要问题和1个“不良做法”问题。

© www.soinside.com 2019 - 2024. All rights reserved.