gpt4 book ai didi

java - 在连接字段定义上使用 @ManyToOne 时,出现 Unresolved 属性异常

转载 作者:行者123 更新时间:2023-12-02 10:30:56 24 4
gpt4 key购买 nike

我有以下@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();
}

...

}

我的存储库接口(interface)定义如下:

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);

关于java - 在连接字段定义上使用 @ManyToOne 时,出现 Unresolved 属性异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53616534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com