我在这个实体之间有一个双向的一对多关系,所有这些都是由 Netbeans 的 JPA 向导创建的
车辆(车辆)
@Entity
@Table(name = "vehiculo")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Vehiculo.findAll", query = "SELECT v FROM Vehiculo v"),
@NamedQuery(name = "Vehiculo.findById", query = "SELECT v FROM Vehiculo v WHERE v.id = :id")})
public class Vehiculo implements Serializable {
//more attributes
@OneToMany(cascade = CascadeType.ALL, mappedBy = "idVehiculo", fetch = FetchType.LAZY)
private List<PuntoTrayecto> puntoTrayectoList;
//methods
}
PuntoTrayecto (TrayectoryPoint)
@Entity
@Table(name = "punto_trayecto")
@XmlRootElement
public class PuntoTrayecto implements Serializable {
//more attributes
@JoinColumn(name = "id_vehiculo", referencedColumnName = "id")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Vehiculo idVehiculo;
}
我需要通过 id 获取“Vehiculo”,因此我将 NamedQuery 称为“Vehiculo.findById”
Query query = em.createNamedQuery("Vehiculo.findById");
query.setParameter("id", 1);
Object o = query.getSingleResult();
Vehiculo vehiculo = (Vehiculo)o;
我得到这个例外:
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'precision, tiempo, velocidad, id_vehiculo FROM punto_trayecto WHERE (id_vehiculo' at line 1
这是试图调用的查询
Call: SELECT id, latitud, longitud, precision, tiempo, velocidad, id_vehiculo FROM punto_trayecto WHERE (id_vehiculo = ?)
它对应于另一个实体“PuntoTrayecto”,我猜这个 SELECT 查询被调用来填充“Vehiculo”的列表 puntoTrayectoList,如果我使用 JPA 控制器也会发生同样的情况。怎么了?