具有自定义查询和复合主键的 JpaRepository:错误未知列
JpaRepository with custom Query and Composite Primary Key: error unknown column
我有一个带有复合主键的 SQL 服务器实体。
这就是实体:
@Entity
@Table(name = "plz", schema = "dbo")
@IdClass(plzId.class)
public class Plz {
@Id
@Column(name = "plz", columnDefinition = "NCHAR(5)")
private String plz;
@Id
@Column(name = "ort", columnDefinition = "NCHAR(30)")
private String ort;
@Column(name = "gueltigbis")
private Date gueltigBis;
@Column(name = "gueltigvon")
private Date gueltigVon;
@Id
@Column(name = "value", columnDefinition = "NCHAR(5)")
private String value;
}
这是 ID Class:
@Data
@EqualsAndHashCode
public class plzId implements Serializable {
private String plz;
private String ort;
private String value;
}
那就是JpaRepository
public interface PlzRepository extends JpaRepository<Plz, PlzId> {
@Query(value = "SELECT value, ort FROM dbo.plz WHERE plz = :plz AND gueltigvon <= :bezugsdatum AND gueltigbis >= :bezugsdatum",
nativeQuery = true
)
List<Plz> findByPlzDatum(@Param("plz") String plz, @Param("bezugsdatum") Date bezugsdatum);
}
但是当执行这个查询时我得到一个错误:
列名无效:plz
终于找到解决方案了:-)
select 没有返回完整的键/对象。对象验证失败,不存在合适的构造函数。
我有一个带有复合主键的 SQL 服务器实体。
这就是实体:
@Entity
@Table(name = "plz", schema = "dbo")
@IdClass(plzId.class)
public class Plz {
@Id
@Column(name = "plz", columnDefinition = "NCHAR(5)")
private String plz;
@Id
@Column(name = "ort", columnDefinition = "NCHAR(30)")
private String ort;
@Column(name = "gueltigbis")
private Date gueltigBis;
@Column(name = "gueltigvon")
private Date gueltigVon;
@Id
@Column(name = "value", columnDefinition = "NCHAR(5)")
private String value;
}
这是 ID Class:
@Data
@EqualsAndHashCode
public class plzId implements Serializable {
private String plz;
private String ort;
private String value;
}
那就是JpaRepository
public interface PlzRepository extends JpaRepository<Plz, PlzId> {
@Query(value = "SELECT value, ort FROM dbo.plz WHERE plz = :plz AND gueltigvon <= :bezugsdatum AND gueltigbis >= :bezugsdatum",
nativeQuery = true
)
List<Plz> findByPlzDatum(@Param("plz") String plz, @Param("bezugsdatum") Date bezugsdatum);
}
但是当执行这个查询时我得到一个错误: 列名无效:plz
终于找到解决方案了:-) select 没有返回完整的键/对象。对象验证失败,不存在合适的构造函数。