我正在尝试在Hibernate中构建一个小项目,并使用mysql docker数据库进行演示。一切正常,直到我添加了一个名为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&useSSL=false&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 :(得分:0)
Sadly I could not figure out what the problem was. So I just recreated the Project copy and pasted the whole code and everything worked fine.
答案 1 :(得分:0)
您缺少一个用
注释的字段@Id
删除
@Entity(name = "Flight")
到
@Entity
和
import javax.persistence.*;