このエンティティ間に双方向の 1 対多の関係があります。これはすべて、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 コントローラーを使用すると同じことが起こります。どうしたの?