为了账号安全,请及时绑定邮箱和手机立即绑定

错误查找对象 Spring JPA:“通过持久属性的反射访问字段时出错”

错误查找对象 Spring JPA:“通过持久属性的反射访问字段时出错”

小唯快跑啊 2024-01-17 21:12:08
我有以下课程:@Entity(name = "focusoc_orbit")@Datapublic class OrbitAdapter extends Adapter{  @Id  private String              id;  ...  public String getId() {    return id;  }  public void setId(String id) {    this.id = id;  }  ...}和,@Entity(name = "focusoc_conjunction")@Datapublic class ConjunctionAdapter extends Adapter {  @Id  @GeneratedValue(strategy = GenerationType.IDENTITY)  private Long         id;  @ManyToOne  @JoinColumn(name = "target_id")  private OrbitAdapter target;  @ManyToOne  @JoinColumn(name = "chaser_id")  private OrbitAdapter chaser;  ...  public OrbitAdapter getTarget(){    return target;  }  public void setTarget(OrbitAdapter target){    this.target = target;  }  public String getChaserId(){    return chaserId;  }  public void setChaser(OrbitAdapter chaser){    this.chaser = chaser;  }  ...}我还定义了存储库:public interface ConjunctionRepository extends PagingAndSortingRepository<ConjunctionAdapter, Long> {  public ConjunctionAdapter findByTargetAndChaserAndTimeOfCloseApproach(String target, String chaser, Date timeOfCloseApproach);}当我尝试拨打电话时,ConjunctionAdapter c = conjunctionRepository.findByTargetAndChaserAndTimeOfCloseApproach(targetId, chaserId, timeOfCloseApproach());它返回错误:org.hibernate.property.access.spi.PropertyAccessException:通过持久属性 [gcs.fds.focusoc.adapter.OrbitAdapter# 的反射访问字段 [private java.lang.String gcs.fds.focusoc.adapter.OrbitAdapter.id] 时出错id]:02035A”我尝试了很多不同的解决方案,但发现它对我不起作用。有什么帮助吗?
查看完整描述

1 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

我解决了!


将@Table类的注释@Data更改为表名。所以,


@Entity

@Table(name = "focusoc_orbit")

public class OrbitAdapter extends Adapter{


  @Id

  private String              id;


  ...


  public String getId() {

    return id;

  }


  public void setId(String id) {

    this.id = id;

  }


  ...


}

和,


@Entity

@Table(name = "focusoc_conjunction")

public class ConjunctionAdapter extends Adapter {


  @Id

  @GeneratedValue(strategy = GenerationType.IDENTITY)

  private Long         id;


  @ManyToOne

  @JoinColumn(name = "target_id")

  private OrbitAdapter target;


  @ManyToOne

  @JoinColumn(name = "chaser_id")

  private OrbitAdapter chaser;


  ...

  public OrbitAdapter getTarget(){

    return target;

  }

  public void setTarget(OrbitAdapter target){

    this.target = target;

  }

  public String getChaserId(){

    return chaserId;

  }

  public void setChaser(OrbitAdapter chaser){

    this.chaser = chaser;

  }


  ...


}

这样做会返回错误,


java.lang.IllegalArgumentException:参数值 [02035A] 与预期类型不匹配 [gcs.fds.focusoc.adapter.OrbitAdapter (n/a)]


所以我在Repository中添加了注释@Query来指定必须在数据库中搜索哪些内容,


public interface ConjunctionRepository extends PagingAndSortingRepository<ConjunctionAdapter, Long> {


  @Query("SELECT c FROM ConjunctionAdapter c WHERE c.target.id = :target AND c.chaser.id = :chaser AND c.timeOfCloseApproach = :timeOfCloseApproach")

  public ConjunctionAdapter findByTargetAndChaserAndTimeOfCloseApproach(String target, String chaser, Date timeOfCloseApproach);

}

并解决了!


查看完整回答
反对 回复 2024-01-17
  • 1 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信