Я пытаюсь создать небольшой проект в Hibernate и делаю демонстрацию с базой данных докеров mysql. Все работало нормально, пока я не добавил новый класс под названием Flight и соответствующий FlightController. В журнале написано:

2018-12-02 14:42:47,281 DEBUG [main] org.hibernate.cfg.AbstractPropertyHolder: Attempting to locate auto-apply AttributeConverter for property [de.hsma.jens.models.Customer:address]
2018-12-02 14:42:47,281 DEBUG [main] org.hibernate.cfg.annotations.SimpleValueBinder: building SimpleValue for address
2018-12-02 14:42:47,281 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: Building property address
2018-12-02 14:42:47,281 DEBUG [main] org.hibernate.cfg.Ejb3Column: Binding column: Ejb3Column{table=org.hibernate.mapping.Table(CUSTOMERS), mappingColumn=country, insertable=true, updatable=true, unique=false}
2018-12-02 14:42:47,281 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: MetadataSourceProcessor property country with lazy=false
2018-12-02 14:42:47,281 DEBUG [main] org.hibernate.cfg.AbstractPropertyHolder: Attempting to locate auto-apply AttributeConverter for property [de.hsma.jens.models.Customer:country]
2018-12-02 14:42:47,282 DEBUG [main] org.hibernate.cfg.annotations.SimpleValueBinder: building SimpleValue for country
2018-12-02 14:42:47,282 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: Building property country
2018-12-02 14:42:47,282 DEBUG [main] org.hibernate.cfg.Ejb3Column: Binding column: Ejb3Column{table=org.hibernate.mapping.Table(CUSTOMERS), mappingColumn=creditCard, insertable=true, updatable=true, unique=false}
2018-12-02 14:42:47,282 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: MetadataSourceProcessor property creditCard with lazy=false
2018-12-02 14:42:47,282 DEBUG [main] org.hibernate.cfg.AbstractPropertyHolder: Attempting to locate auto-apply AttributeConverter for property [de.hsma.jens.models.Customer:creditCard]
2018-12-02 14:42:47,282 DEBUG [main] org.hibernate.cfg.annotations.SimpleValueBinder: building SimpleValue for creditCard
2018-12-02 14:42:47,283 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: Building property creditCard
2018-12-02 14:42:47,283 DEBUG [main] org.hibernate.cfg.Ejb3Column: Binding column: Ejb3Column{table=org.hibernate.mapping.Table(CUSTOMERS), mappingColumn=firstname, insertable=true, updatable=true, unique=false}
2018-12-02 14:42:47,283 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: MetadataSourceProcessor property firstname with lazy=false
2018-12-02 14:42:47,286 DEBUG [main] org.hibernate.cfg.AbstractPropertyHolder: Attempting to locate auto-apply AttributeConverter for property [de.hsma.jens.models.Customer:firstname]
2018-12-02 14:42:47,286 DEBUG [main] org.hibernate.cfg.annotations.SimpleValueBinder: building SimpleValue for firstname
2018-12-02 14:42:47,286 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: Building property firstname
2018-12-02 14:42:47,287 DEBUG [main] org.hibernate.cfg.Ejb3Column: Binding column: Ejb3Column{table=org.hibernate.mapping.Table(CUSTOMERS), mappingColumn=lastname, insertable=true, updatable=true, unique=false}
2018-12-02 14:42:47,287 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: MetadataSourceProcessor property lastname with lazy=false
2018-12-02 14:42:47,287 DEBUG [main] org.hibernate.cfg.AbstractPropertyHolder: Attempting to locate auto-apply AttributeConverter for property [de.hsma.jens.models.Customer:lastname]
2018-12-02 14:42:47,287 DEBUG [main] org.hibernate.cfg.annotations.SimpleValueBinder: building SimpleValue for lastname
2018-12-02 14:42:47,287 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: Building property lastname
2018-12-02 14:42:47,287 DEBUG [main] org.hibernate.cfg.Ejb3Column: Binding column: Ejb3Column{table=org.hibernate.mapping.Table(CUSTOMERS), mappingColumn=miles_flown_year, insertable=true, updatable=true, unique=false}
2018-12-02 14:42:47,287 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: MetadataSourceProcessor property miles_flown_year with lazy=false
2018-12-02 14:42:47,287 DEBUG [main] org.hibernate.cfg.AbstractPropertyHolder: Attempting to locate auto-apply AttributeConverter for property [de.hsma.jens.models.Customer:miles_flown_year]
2018-12-02 14:42:47,287 DEBUG [main] org.hibernate.cfg.annotations.SimpleValueBinder: building SimpleValue for miles_flown_year
2018-12-02 14:42:47,287 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: Building property miles_flown_year
2018-12-02 14:42:47,287 DEBUG [main] org.hibernate.cfg.Ejb3Column: Binding column: Ejb3Column{table=org.hibernate.mapping.Table(CUSTOMERS), mappingColumn=phonenumber, insertable=true, updatable=true, unique=false}
2018-12-02 14:42:47,287 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: MetadataSourceProcessor property phonenumber with lazy=false
2018-12-02 14:42:47,288 DEBUG [main] org.hibernate.cfg.AbstractPropertyHolder: Attempting to locate auto-apply AttributeConverter for property [de.hsma.jens.models.Customer:phonenumber]
2018-12-02 14:42:47,288 DEBUG [main] org.hibernate.cfg.annotations.SimpleValueBinder: building SimpleValue for phonenumber
2018-12-02 14:42:47,288 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: Building property phonenumber
2018-12-02 14:42:47,288 DEBUG [main] org.hibernate.cfg.Ejb3Column: Binding column: Ejb3Column{table=org.hibernate.mapping.Table(CUSTOMERS), mappingColumn=status, insertable=true, updatable=true, unique=false}
2018-12-02 14:42:47,288 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: MetadataSourceProcessor property status with lazy=false
2018-12-02 14:42:47,288 DEBUG [main] org.hibernate.cfg.AbstractPropertyHolder: Attempting to locate auto-apply AttributeConverter for property [de.hsma.jens.models.Customer:status]
2018-12-02 14:42:47,290 DEBUG [main] org.hibernate.cfg.annotations.SimpleValueBinder: building SimpleValue for status
2018-12-02 14:42:47,290 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: Building property status
2018-12-02 14:42:47,290 DEBUG [main] org.hibernate.cfg.Ejb3Column: Binding column: Ejb3Column{table=org.hibernate.mapping.Table(CUSTOMERS), mappingColumn=total_miles_flown, insertable=true, updatable=true, unique=false}
2018-12-02 14:42:47,290 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: MetadataSourceProcessor property total_miles_flown with lazy=false
2018-12-02 14:42:47,290 DEBUG [main] org.hibernate.cfg.AbstractPropertyHolder: Attempting to locate auto-apply AttributeConverter for property [de.hsma.jens.models.Customer:total_miles_flown]
2018-12-02 14:42:47,290 DEBUG [main] org.hibernate.cfg.annotations.SimpleValueBinder: building SimpleValue for total_miles_flown
2018-12-02 14:42:47,291 DEBUG [main] org.hibernate.cfg.annotations.PropertyBinder: Building property total_miles_flown
2018-12-02 14:42:47,291 DEBUG [main] org.hibernate.cfg.AnnotationBinder: Binding entity from annotated class: de.hsma.jens.models.Flight
2018-12-02 14:42:47,291 DEBUG [main] org.hibernate.cfg.Ejb3Column: Binding column: Ejb3DiscriminatorColumn{logicalColumnName'DTYPE', discriminatorTypeName='string'}
2018-12-02 14:42:47,291 DEBUG [main] org.hibernate.cfg.annotations.EntityBinder: Import with entity name Flight
2018-12-02 14:42:47,291 DEBUG [main] org.hibernate.cfg.annotations.EntityBinder: Bind entity de.hsma.jens.models.Flight on table FLIGHTS
Exception in thread "main" java.lang.ExceptionInInitializerError
2018-12-02 14:42:47,293 DEBUG [main] org.hibernate.jpa.HibernatePersistenceProvider: Unable to build entity manager factory
Caused by: javax.persistence.PersistenceException: Unable to build entity manager factory
org.hibernate.AnnotationException: No identifier specified for entity: de.hsma.jens.models.Flight
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:66)
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:265)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:211)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:719)
    at de.hsma.jens.de.hsma.jens.transactions.UpdateUncommitted1.<clinit>(UpdateUncommitted1.java:22)
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245)
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: de.hsma.jens.models.Flight
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:265)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:211)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:848)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:719)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:875)
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:222)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:848)
    at de.hsma.jens.de.hsma.jens.transactions.UpdateUncommitted1.<clinit>(UpdateUncommitted1.java:22)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:875)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
    ... 3 more
2018-12-02 14:43:16,898 DEBUG [pool-2-thread-1] org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProvider

Другие решения, представленные в аналогичном вопросе, не помогли и не подошли к конкретной проблеме, которую я имею.

Класс Flight выглядит так:

package de.hsma.jens.models;

import com.google.common.base.Objects;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

@Entity(name = "Flight")
@Table(name = "FLIGHTS")
public class Flight {
    @Id
    //@GeneratedValue(generator = "uuid")
    //@GenericGenerator(name = "uuid", strategy = "uuid2")
    private String flightID;

    private String departureTime;
    private String timeOfArrival;
    private String type;
    private int priceFirstClass;
    private int priceEcoClass;
    private int seatsEco;
    private int seatsFirstClass;

    public Flight() {
    }

    public String getFlightID() {
        return flightID;
    }

    public void setFlightID(String flightID) {
        this.flightID = flightID;
    }

    public String getDepartureTime() {
        return departureTime;
    }

    public void setDepartureTime(String departureTime) {
        this.departureTime = departureTime;
    }

    public String getTimeOfArrival() {
        return timeOfArrival;
    }

    public void setTimeOfArrival(String timeOfArrival) {
        this.timeOfArrival = timeOfArrival;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPriceFirstClass() {
        return priceFirstClass;
    }

    public void setPriceFirstClass(int priceFirstClass) {
        this.priceFirstClass = priceFirstClass;
    }

    public int getPriceEcoClass() {
        return priceEcoClass;
    }

    public void setPriceEcoClass(int priceEcoClass) {
        this.priceEcoClass = priceEcoClass;
    }

    public int getSeatsEco() {
        return seatsEco;
    }

    public void setSeatsEco(int seatsEco) {
        this.seatsEco = seatsEco;
    }

    public int getSeatsFirstClass() {
        return seatsFirstClass;
    }

    public void setSeatsFirstClass(int seatsFirstClass) {
        this.seatsFirstClass = seatsFirstClass;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .add("flightID", flightID)
                .add("departureTime", departureTime)
                .add("timeOfArrival", timeOfArrival)
                .add("type", type)
                .add("priceFirstClass", priceFirstClass)
                .add("priceEcoClass", priceEcoClass)
                .add("seatsEco", seatsEco)
                .add("seatsFirstClass", seatsFirstClass)
                .toString();
    }
}

Как видите, я попытался использовать аннотацию @GeneratedValue, но это не помогло, и ошибка по-прежнему следующая: «Для объекта не указан идентификатор: de.hsma.jens.models.Flight.

Мой файл persistence.xml выглядит так:

<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/persistence"
             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="ogm-mysql" transaction-type="JTA">
            <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
            <properties>
                <property name="hibernate.archive.autodetection" value="class" />
                <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
                <property name="hibernate.connection.url"
                          value="jdbc:mysql://localhost:3306/ogm_hibernate?createDatabaseIfNotExist=true" />
                <property name="hibernate.connection.username" value="root" />
                <property name="hibernate.connection.password" value="jenskohler" />

                <property name="hibernate.flushMode" value="FLUSH_AUTO" />
                <property name="hibernate.hbm2ddl.auto" value="create" />
                <property name="hibernate.show_sql" value="true" />
            </properties>
        </persistence-unit>
    -->

    <persistence-unit name="OGM_MYSQL" transaction-type="JTA">
        <description>
            Persistence unit for the JPA tutorial of the Hibernate Getting Started Guide
        </description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>de.hsma.jens.models.Customer</class>
        <class>de.hsma.jens.models.Airport</class>
        <class>de.hsma.jens.models.Flight</class>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url"
                      value="jdbc:mysql://localhost:3306/ogm_hibernate?createDatabaseIfNotExist=true&amp;useSSL=false&amp;allowPublicKeyRetrieval=true"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="root"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

Кроме того, файл pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>hibernate-ogm-example</groupId>
    <artifactId>hibernate-ogm-example</artifactId>
    <version>1.0-SNAPSHOT</version>
   <!-- use can use either 'jar' or 'war' here -->
    <packaging>jar</packaging>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>


        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                    </manifest>
                </archive>

                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>


        </plugins>
    </build>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.hibernate.ogm</groupId>
                <artifactId>hibernate-ogm-bom</artifactId>
                <version>5.1.0.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <dependencies>
        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-infinispan</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-search-orm</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-neo4j</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-mongodb</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-cassandra</artifactId>

        </dependency>


        <dependency>
            <groupId>org.hibernate.ogm</groupId>
            <artifactId>hibernate-ogm-redis</artifactId>

        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.jboss.spec.javax.transaction/jboss-transaction-api_1.2_spec -->
        <dependency>
            <groupId>org.jboss.spec.javax.transaction</groupId>
            <artifactId>jboss-transaction-api_1.2_spec</artifactId>
        </dependency>


        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.jboss.narayana.jta/narayana-jta -->
        <dependency>
            <groupId>org.jboss.narayana.jta</groupId>
            <artifactId>narayana-jta</artifactId>
            <version>5.4.0.Final</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.jboss/jboss-transaction-spi -->
        <dependency>
            <groupId>org.jboss</groupId>
            <artifactId>jboss-transaction-spi</artifactId>
            <version>7.6.0.Final</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>


    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>
0
Patrick.H 2 Дек 2018 в 16:57

2 ответа

Лучший ответ

К сожалению, я не мог понять, в чем проблема. Поэтому я просто воссоздал копию проекта и вставил весь код, и все работало нормально.

0
Patrick.H 3 Дек 2018 в 12:59

Вам не хватает поля с пометкой

@Id

Удалять

@Entity(name = "Flight")

Чтобы

@Entity

А также

import javax.persistence.*;
0
superup 10 Апр 2021 в 10:00