在连接字段定义上使用 @ManyToOne 时,出现未解决的 属性 异常
When using @ManyToOne on join field definition, I get an unresolved property exception
我有以下@Entity:
@Entity
@Table(name = "devices")
public class Devices extends Model {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "asset_tag")
private Integer asset_tag;
@Column(name = "token")
private String token;
@Column(name = "last_seen")
private String last_seen;
@OneToMany(
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.LAZY,
mappedBy = "device"
)
private Set<Tracking> trackings;
public Devices() {
super();
}
public Devices(Long id, String name, Integer asset_tag, String token, String last_seen) {
super();
this.id = id;
this.name = name;
this.asset_tag = asset_tag;
this.token = token;
this.last_seen = last_seen;
}
public String getName() {
return name;
}
public Devices setName(String name) {
this.name = name;
return this;
}
public Integer getAsset_tag() {
return asset_tag;
}
public Devices setAsset_tag(Integer asset_tag) {
this.asset_tag = asset_tag;
return this;
}
public String getToken() {
return token;
}
public Devices setToken(String token) {
this.token = token;
return this;
}
public String getLast_seen() {
return last_seen;
}
public Devices setLast_seen(String last_seen) {
this.last_seen = last_seen;
return this;
}
public Set<Tracking> getTrackings() {
return trackings;
}
public Devices setTrackings(Set<Tracking> trackings) {
this.trackings = trackings;
return this;
}
}
@Entity
@Table(name = "tracking")
public class Tracking extends Model {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "reported_lat")
private Float reported_lat;
@Column(name = "reported_lng")
private Float reported_lng;
@Column(name = "normalized_lat")
private Float normalized_lat;
@Column(name = "normalized_lng")
private Float normalized_lng;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "device_id")
private Devices device;
public Tracking() {
super();
}
...
}
我的存储库接口是这样定义的:
public interface TrackingRepository<A> extends JpaRepository<Tracking, Long> {
@Query("SELECT t FROM Tracking t WHERE t.device_id = :device_id ORDER BY t.created DESC")
List<Tracking> getLastLocation(@Param("device_id") Long device_id, Pageable pageable);
@Query("SELECT t FROM Tracking t WHERE t.device_id = :device_id AND t.created >= :time_limit ORDER BY t.created DESC")
List<Tracking> getLastLocation(@Param("device_id") Long device_id, @Param("time_limit") Integer time_limit, Pageable pageable);
}
当我构建我的项目时,当它构建存储库时 类,我得到以下异常:
Caused by: org.hibernate.QueryException: could not resolve property: device_id of: com.vw.shuttle.model.Tracking [SELECT t FROM com.vw.shuttle.model.Tracking t WHERE t.device_id = :device_id ORDER BY t.created DESC]
at org.hibernate.QueryException.generateQueryException(QueryException.java:120)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:217)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:553)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662)`
为什么我得到那个异常?从我阅读的文档来看,它似乎应该通过 @ManyToOne
和 @JoinColumn
注释处理该映射。
您的查询应该根据对象属性而不是列来定义,即 t.device.id
而不是 t.device_id
@Query("SELECT t FROM Tracking t WHERE t.device.id = :device_id AND t.created
>= :time_limit ORDER BY t.created DESC")
List<Tracking> getLastLocation(@Param("device_id") Long device_id, @Param("time_limit") Integer time_limit,
Pageable pageable);
我有以下@Entity:
@Entity
@Table(name = "devices")
public class Devices extends Model {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "asset_tag")
private Integer asset_tag;
@Column(name = "token")
private String token;
@Column(name = "last_seen")
private String last_seen;
@OneToMany(
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.LAZY,
mappedBy = "device"
)
private Set<Tracking> trackings;
public Devices() {
super();
}
public Devices(Long id, String name, Integer asset_tag, String token, String last_seen) {
super();
this.id = id;
this.name = name;
this.asset_tag = asset_tag;
this.token = token;
this.last_seen = last_seen;
}
public String getName() {
return name;
}
public Devices setName(String name) {
this.name = name;
return this;
}
public Integer getAsset_tag() {
return asset_tag;
}
public Devices setAsset_tag(Integer asset_tag) {
this.asset_tag = asset_tag;
return this;
}
public String getToken() {
return token;
}
public Devices setToken(String token) {
this.token = token;
return this;
}
public String getLast_seen() {
return last_seen;
}
public Devices setLast_seen(String last_seen) {
this.last_seen = last_seen;
return this;
}
public Set<Tracking> getTrackings() {
return trackings;
}
public Devices setTrackings(Set<Tracking> trackings) {
this.trackings = trackings;
return this;
}
}
@Entity
@Table(name = "tracking")
public class Tracking extends Model {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "reported_lat")
private Float reported_lat;
@Column(name = "reported_lng")
private Float reported_lng;
@Column(name = "normalized_lat")
private Float normalized_lat;
@Column(name = "normalized_lng")
private Float normalized_lng;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "device_id")
private Devices device;
public Tracking() {
super();
}
...
}
我的存储库接口是这样定义的:
public interface TrackingRepository<A> extends JpaRepository<Tracking, Long> {
@Query("SELECT t FROM Tracking t WHERE t.device_id = :device_id ORDER BY t.created DESC")
List<Tracking> getLastLocation(@Param("device_id") Long device_id, Pageable pageable);
@Query("SELECT t FROM Tracking t WHERE t.device_id = :device_id AND t.created >= :time_limit ORDER BY t.created DESC")
List<Tracking> getLastLocation(@Param("device_id") Long device_id, @Param("time_limit") Integer time_limit, Pageable pageable);
}
当我构建我的项目时,当它构建存储库时 类,我得到以下异常:
Caused by: org.hibernate.QueryException: could not resolve property: device_id of: com.vw.shuttle.model.Tracking [SELECT t FROM com.vw.shuttle.model.Tracking t WHERE t.device_id = :device_id ORDER BY t.created DESC]
at org.hibernate.QueryException.generateQueryException(QueryException.java:120)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:217)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:553)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662)`
为什么我得到那个异常?从我阅读的文档来看,它似乎应该通过 @ManyToOne
和 @JoinColumn
注释处理该映射。
您的查询应该根据对象属性而不是列来定义,即 t.device.id
而不是 t.device_id
@Query("SELECT t FROM Tracking t WHERE t.device.id = :device_id AND t.created
>= :time_limit ORDER BY t.created DESC")
List<Tracking> getLastLocation(@Param("device_id") Long device_id, @Param("time_limit") Integer time_limit,
Pageable pageable);