我在这些实体之间有一个双向的一对多关系,所有这些都是由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”的列表PuntoTrayecto List,如果我使用JPA控制器,也会发生同样的情况。发生了什么?

最佳答案

问题可能是由列precision引起的。这是一个保留字(according to MySQL)。您可以检查this answer以获得解决方法

08-28 17:58