我正在尝试从SQLite数据库中检索一些数据。我正在使用JPA和EclipseLink作为提供程序,我正在尝试构建REST API。我有我的模型类,我在其中查询的类。 servcie和我构建URI的其余类。
这是我的模特课:
package com.vehicle.dao;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.vehicle.model.Vehicle;
public class VehicleDAO {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "example" );
EntityManager em = emfactory.createEntityManager( );
public void fetchVehicle(){
Query query = em.createQuery("Select v.name, v.model, v.purchasing_date from Vehicles v");
List<String> list = query.getResultList();
for(String e:list) {
System.out.println("Vehicle NAME :"+e);
}
}
}
这是我的DAO课程:
package com.vehicle.service;
import java.util.ArrayList;
import com.vehicle.dao.VehicleDAO;
import com.vehicle.model.Vehicle;
public class VehicleService {
VehicleDAO vehicleDao = new VehicleDAO();
public void getDefaultVehicle() {
System.out.println("BLAAAAAA");
vehicleDao.fetchVehicle();
}
}
这是从我的DAO类调用该方法的服务类:
package com.vehicle.rest;
import java.util.ArrayList;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.vehicle.model.Vehicle;
import com.vehicle.service.VehicleService;
@Path("/vehicles")
public class VehicleRest {
VehicleService vehicleService = new VehicleService();
@GET
@Path("/getVehicles")
@Produces(MediaType.APPLICATION_JSON)
public void getDefaultVehicleInJSON() {
System.out.println("BLAAAAAAA");
vehicleService.getDefaultVehicle();
}
}
这个类是我构建其余URI的类:
Aug 08, 2017 6:08:27 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Problem compiling [Select v.name, v.model, v.purchasing_date from Vehicles v].
[7, 13] The state field path 'v.name' cannot be resolved to a valid type.
[15, 22] The state field path 'v.model' cannot be resolved to a valid type.
[24, 41] The state field path 'v.purchasing_date' cannot be resolved to a valid type.
[47, 55] The abstract schema type 'Vehicles' is unknown.
问题是,当我尝试在浏览器中运行它时,它会给我这个错误:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.vehicle.model.Vehicle</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlite:C:/development.sqlite3" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.ddl-generation" value="create-tables" />
</properties>
</persistence-unit>
</persistence>
我不明白为什么。没有架构,它只是默认架构。
修改
这是我的persistence.xml:
CustomerView
提前致谢!
答案 0 :(得分:1)
查询应为
Select v.name, v.model, v.purchasing_date from Vehicle v
您在问题中加了Vehicles
。实体名称为Vehicle
(与班级相同)。