问题
So basically as in title, my JPQL query created with entityManager.createQuery()
method always returns empty list, which is strange because there certainly exists underlying data. I have a View object in SQL Server database which maps several tables into one view using union all
. Here is my JPQL string passed to earlier mentioned method createQuery()
:
SELECT t
FROM TransitView t
LEFT OUTER JOIN t.marca marca
LEFT OUTER JOIN t.modello modello
LEFT OUTER JOIN t.serie serie
LEFT OUTER JOIN t.colore colore
WHERE t.corsia.attiva = TRUE
AND t.istante BETWEEN :istanteInizio AND :istanteFine
AND (t.partizione = :partizione0 OR t.partizione = :partizione1 )
AND (t.corsia.id = :corsia0 )
ORDER BY t.istante DESC
Before you ask, yes all the parameters are set and there is a data. All the parameters also do point to correct records that do exist in database. The above code was working fine for as long as I have been using Tables in underlying database instead of views. Now when instead of using table I do use a View (which supposedly ought to work just as well) I get only empty list each time. I do avoid presenting more code for the reasons of clarity, please trust me - there is no issue with database being empty or perhaps parameters being set to wrong values. I checked multiple of times. TransitView
is a JPA entity class representing the aforementioned View in database. Here is how it looks:
package it.bsec.target.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.eclipse.persistence.annotations.Convert;
import com.vividsolutions.jts.geom.Point;
@Entity
@Table(name="dbo.TransitiView")
@SuppressWarnings("serial")
@SecondaryTable(name = "TransitiPunti", pkJoinColumns=@PrimaryKeyJoinColumn(name = "IdTransiti"))
public class TransitView implements Serializable {
@Id
@Column(name = "Id")
private Long id;
@Column(name = "Targa", length = 15)
private String targa;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "Istante")
private Date istante;
@ManyToOne
@JoinColumn(name = "IdCorsia")
private CorsiaSQLServer corsia;
@ManyToOne
@JoinColumn(name = "IdNazionalita")
private Nazionalita nazionalita;
@ManyToOne
@JoinColumn(name = "IdTipologiaVeicolo")
private TipologiaVeicolo tipologiaVeicolo;
@Column(name = "Confidenza")
private int confidenza;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "IstanteRicezione")
private Date istanteRicezione;
@ManyToOne
@JoinColumn(name = "IdMarca")
private Marca marca;
@ManyToOne
@JoinColumn(name = "IdModello")
private Modello modello;
@ManyToOne
@JoinColumn(name = "IdSerie")
private Serie serie;
@Column(name = "TargaSecondaria", length = 15)
private String targaSecondaria;
@ManyToOne
@JoinColumn(name = "IdColore")
private Colore colore;
@Column(name = "VelocitaStimata")
private Float velocitaStimata;
@Column(name = "CodiceCaricoPericoloso", length = 4)
private String codiceCaricoPericoloso;
@Column(name = "UtmcInstanceId")
private Long utmcInstanceId;
@Column(name = "Partizione")
private int partizione;
@Column(name = "SQLServer_longitude", table="TransitiPunti")
private Double longitude;
@Column(name = "SQLServer_latitude", table="TransitiPunti")
private Double latitude;
@Transient
private int numberOfTransits;
public TransitView() {}
public TransitView(String targa, Date istante, CorsiaSQLServer corsia, Nazionalita nazionalita, TipologiaVeicolo tipologiaVeicolo, int confidenza, int partizione) {
super();
this.targa = targa;
this.istante = istante;
this.corsia = corsia;
this.nazionalita = nazionalita;
this.tipologiaVeicolo = tipologiaVeicolo;
this.confidenza = confidenza;
this.istanteRicezione = new Date(System.currentTimeMillis());
this.partizione = partizione;
}
public TransitView( String plate, int numberOfTransits ) {
super();
targa = plate;
this.setNumberOfTransits(numberOfTransits);
}
public TransitView(Long id2, Date istanteRicezione2, String targa2,
Long utmcInstanceId2, Integer partizione2, Integer confidence,
Date istante2, Float velocitaStimata2, String targaSecondaria2,
String codiceCaricoPericoloso2, Marca marca2, Colore colore2,
Modello modello2, CorsiaSQLServer corsia2,
Nazionalita nazionalita2, TipologiaVeicolo tipologiaVeicolo2,
Serie serie2) {
this.id = id2;
this.istanteRicezione = istanteRicezione2;
this.targa = targa2;
this.utmcInstanceId = utmcInstanceId2;
this.partizione = partizione2;
this.confidenza = confidence;
this.codiceCaricoPericoloso = codiceCaricoPericoloso2;
this.marca = marca2;
this.colore = colore2;
this.modello = modello2;
this.corsia = corsia2;
this.nazionalita = nazionalita2;
this.tipologiaVeicolo = tipologiaVeicolo2;
this.serie = serie2;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTarga() {
return targa;
}
public void setTarga(String targa) {
this.targa = targa;
}
public Date getIstante() {
return istante;
}
public void setIstante(Date istante) {
this.istante = istante;
}
public Nazionalita getNazionalita() {
return nazionalita;
}
public void setNazionalita(Nazionalita nazionalita) {
this.nazionalita = nazionalita;
}
public TipologiaVeicolo getTipologiaVeicolo() {
return tipologiaVeicolo;
}
public void setTipologiaVeicolo(TipologiaVeicolo tipologiaVeicolo) {
this.tipologiaVeicolo = tipologiaVeicolo;
}
public int getConfidenza() {
return confidenza;
}
public void setConfidenza(int confidenza) {
this.confidenza = confidenza;
}
public Date getIstanteRicezione() {
return istanteRicezione;
}
public void setIstanteRicezione(Date istanteRicezione) {
this.istanteRicezione = istanteRicezione;
}
public Marca getMarca() {
return marca;
}
public void setMarca(Marca marca) {
this.marca = marca;
}
public Modello getModello() {
return modello;
}
public void setModello(Modello modello) {
this.modello = modello;
}
public Serie getSerie() {
return serie;
}
public void setSerie(Serie serie) {
this.serie = serie;
}
public String getTargaSecondaria() {
return targaSecondaria;
}
public void setTargaSecondaria(String targaSecondaria) {
this.targaSecondaria = targaSecondaria;
}
public Colore getColore() {
return colore;
}
public void setColore(Colore colore) {
this.colore = colore;
}
public Float getVelocitaStimata() {
return velocitaStimata;
}
public void setVelocitaStimata(Float velocitaStimata) {
this.velocitaStimata = velocitaStimata;
}
public String getCodiceCaricoPericoloso() {
return codiceCaricoPericoloso;
}
public void setCodiceCaricoPericoloso(String codiceCaricoPericoloso) {
this.codiceCaricoPericoloso = codiceCaricoPericoloso;
}
public Long getUtmcInstanceId() {
return utmcInstanceId;
}
public void setUtmcInstanceId(Long utmcInstanceId) {
this.utmcInstanceId = utmcInstanceId;
}
public int getPartizione() {
return partizione;
}
public void setPartizione(int partizione) {
this.partizione = partizione;
}
public CorsiaSQLServer getCorsia() {
return corsia;
}
public void setCorsia(CorsiaSQLServer corsia) {
this.corsia = corsia;
}
@Transient
public int getNumberOfTransits() {
return numberOfTransits;
}
@Transient
public void setNumberOfTransits(int numberOfTransits) {
this.numberOfTransits = numberOfTransits;
}
}
What am I doing wrong?? Perhaps it is something obvious but I just cannot see it, especially since that same code works with a table but why not with a View?
EDIT:
Any ideas, please :-) ???
Thanks to @Abbé Résina suggestion I have found that my actual SQL Query in SQL Server looks like this and it still returns empty list even if I run it manually from within SQL Server Management Studio, it may be something obvious but I cannot see it!:
SELECT t1.Id, t2.IdTransiti, t1.CodiceCaricoPericoloso,
t1.Confidenza, t1.Istante, t1.IstanteRicezione,
t2.SQLServer_latitude, t2.SQLServer_longitude,
t1.Partizione, t1.Targa, t1.TargaSecondaria,
t1.UtmcInstanceId, t1.VelocitaStimata,
t1.IdColore, t1.IdCorsia, t1.IdMarca,
t1.IdModello, t1.IdNazionalita, t1.IdSerie,
t1.IdTipologiaVeicolo
FROM dbo.TransitiView t1
LEFT OUTER JOIN Serie t0 ON (t0.Id = t1.IdSerie)
LEFT OUTER JOIN dbo.Colori t3 ON (t3.Id = t1.IdColore)
LEFT OUTER JOIN Modelli t4 ON (t4.Id = t1.IdModello)
LEFT OUTER JOIN Marche t5 ON (t5.Id = t1.IdMarca),
TransitiPunti t2, Corsie t6
WHERE ((((((t6.Attiva = 1)
AND (t1.Istante
BETWEEN '2015-09-03 00:16:50.693' AND '2015-09-03 23:16:50.693'))
AND ((t1.Partizione = 0) OR (t1.Partizione = 6)))
AND (t1.IdCorsia = 1)) AND (t2.IdTransiti = t1.Id))
AND (t6.Id = t1.IdCorsia)) ORDER BY t1.Istante DESC
This query runs on SQL Server but I still cannot figure out why it returns empty list (also when I run it from Microsoft SQL Server Management Studio, still empty). All the parameters are right
(IdCorsia = 1, Attiva = 1 Partizione = 0, dates are correct ... )
.
来源:https://stackoverflow.com/questions/32379998/does-anyone-know-why-jpql-query-on-a-view-with-sql-server-does-not-work-getres