我在学校Maven项目中使用JPA执行查询时遇到问题。问题是The name "Booking" is not a recognized entity or identifier.
我有一个名为Booking
的表,尽管我在persistence.xml
中有一个引用,但编译器似乎在项目中找不到它。另外,我从一个简单的JPA项目迁移到了具有JPA支持的maven项目,而且我没有遇到任何问题。
Persistence.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd" >
<persistence-unit name="Lab_4_Maven">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>model.Booking</class>
<class>model.Category</class>
<class>model.Flight</class>
<class>model.Passenger</class>
<class>model.Ticket</class>
</persistence-unit>
</persistence>
private static void readEntities(EntityManagerFactory emf) {
EntityManager em;
em = emf.createEntityManager();
em.getTransaction().begin();
try {
//problem is here
Query query = em.createQuery("SELECT x FROM Booking x");
List<?> list = query.getResultList();
if (!list.isEmpty()) {
for (Object object : list) {
Date date = ((Booking)object).getDate();
String paid = ((Booking)object).getPaid();
long passengerCode = ((Booking)object).getPassenger_Code();
BigDecimal flightCode = ((Booking)object).getFlight_Code();
BigDecimal bookingCode = ((Booking)object).getBooking_Code();
System.out.println("PassengerCode = " + passengerCode
+ ", FlightCode = " + flightCode.toString() + ", BookingCode = "
+ bookingCode.toString() + ", Date = " + date.toString()
+ "Paid = " + paid);
}
} else {
System.out.println("No booking");
}
em.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
em.getTransaction().rollback();
} finally {
em.close();
}
}
错误是:
Oct 17, 2019 8:05:16 PM org.hibernate.jpa.boot.internal.PersistenceXmlParser doResolve
INFO: HHH000318: Could not find any META-INF/persistence.xml file in the classpath
123 INFO [main] openjpa.Runtime - OpenJPA dynamically loaded the class enhancer. Any classes that were not enhanced at build time will be enhanced when they are loaded by the JVM.
148 INFO [main] openjpa.Runtime - Starting OpenJPA 3.1.0
230 INFO [main] openjpa.jdbc.JDBC - Using dictionary class "org.apache.openjpa.jdbc.sql.PostgresDictionary".
488 INFO [main] openjpa.jdbc.JDBC - Connected to PostgreSQL version 11.11 using JDBC driver PostgreSQL JDBC Driver version 42.2.8.
<openjpa-3.1.0-rafcec21a1d489dff682a3ce7986fac6a1c80e8e0 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: An error occurred while parsing the query filter "SELECT x FROM Booking x". Error message: The name "Booking" is not a recognized entity or identifier. Known entity names: []
at org.apache.openjpa.kernel.exps.AbstractExpressionBuilder.parseException(AbstractExpressionBuilder.java:118)
at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getClassMetaData(JPQLExpressionBuilder.java:200)
at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.resolveClassMetaData(JPQLExpressionBuilder.java:170)
at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getCandidateMetaData(JPQLExpressionBuilder.java:245)
at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getCandidateMetaData(JPQLExpressionBuilder.java:215)
at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getCandidateType(JPQLExpressionBuilder.java:208)
at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.access$200(JPQLExpressionBuilder.java:81)
at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder$ParsedJPQL.populate(JPQLExpressionBuilder.java:2456)
at org.apache.openjpa.kernel.jpql.JPQLParser.populate(JPQLParser.java:64)
at org.apache.openjpa.kernel.ExpressionStoreQuery.populateFromCompilation(ExpressionStoreQuery.java:175)
at org.apache.openjpa.kernel.QueryImpl.newCompilation(QueryImpl.java:719)
at org.apache.openjpa.kernel.QueryImpl.compilationFromCache(QueryImpl.java:692)
at org.apache.openjpa.kernel.QueryImpl.compileForCompilation(QueryImpl.java:659)
at org.apache.openjpa.kernel.QueryImpl.compileForExecutor(QueryImpl.java:728)
at org.apache.openjpa.kernel.QueryImpl.compile(QueryImpl.java:627)
at org.apache.openjpa.persistence.EntityManagerImpl.createQuery(EntityManagerImpl.java:1168)
at org.apache.openjpa.persistence.EntityManagerImpl.createQuery(EntityManagerImpl.java:1149)
at org.apache.openjpa.persistence.EntityManagerImpl.createQuery(EntityManagerImpl.java:108)
at model.Test.readEntities(Test.java:65)
at model.Test.main(Test.java:27)