Reputation: 811
I am trying to retrieve some data from an SQLite db. I am using JPA with EclipseLink as a provider and I am trying to build a REST API. I have my model classes, the class in which I do my queries. The servcie and the rest class in which I construct my URI.
This is my model class:
package com.vehicle.model;
import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
/**
* The persistent class for the "Vehicles" database table.
*
*/
@Entity
@Table(name="\"Vehicles\"")
@NamedQuery(name="Vehicle.findAll", query="SELECT v FROM Vehicle v")
public class Vehicle implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="\"Model\"")
private String model;
@Column(name="\"Name\"")
private String name;
@Column(name="\"Purchasing_date\"")
private BigDecimal purchasing_date;
public Vehicle() {
}
public String getModel() {
return this.model;
}
public void setModel(String model) {
this.model = model;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPurchasing_date() {
return this.purchasing_date;
}
public void setPurchasing_date(BigDecimal purchasing_date) {
this.purchasing_date = purchasing_date;
}
}
This is my DAO class:
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);
}
}
}
This is the service class that calls the method from my DAO class:
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();
}
}
This class is the one in which I construct my rest URIs:
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();
}
}
The thing is, that when I try to run this in the browser it gives me this error:
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.
I do not get it why. There is no schema, its just the default schema.
EDIT:
This is my persistence.xml:
<?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>
Thanks in advance!
Upvotes: 0
Views: 628
Reputation: 11531
The query should be
Select v.name, v.model, v.purchasing_date from Vehicle v
You put Vehicles
in your question. The entity name is Vehicle
(same as class).
Upvotes: 1