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

时间:2019-10-31 09:46:14

标签: java spring jpa reflection

我有以下课程:

@Entity(name = "focusoc_orbit")
@Data
public 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")
@Data
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;
  }

  ...

}

我还定义了存储库:

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:错误   访问字段[私有java.lang.String   gcs.fds.focusoc.adapter.OrbitAdapter.id]通过反射进行持久化   属性[gcs.fds.focusoc.adapter.OrbitAdapter#id]:02035A“

我尝试了很多发现的不同解决方案,但对我而言不起作用。有帮助吗?

1 个答案:

答案 0 :(得分:0)

我解决了!

使用表名称更改@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)]

因此我将注释@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);
}

解决了!