我試圖RecordData從我的存盤庫類回傳一個自定義物件,但它試圖實體化我的主Record類。我試過使用Interface Projections,但結果是一樣的。
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
@Table
public class Record {
public record RecordData(long created_at, double value) {
}
@PrimaryKey
private final long sensor_id;
private final long created_at;
private final double value;
public Record(long sensor_id, long created_at, double value) {
this.sensor_id = sensor_id;
this.created_at = created_at;
this.value = value;
}
public long getSensorId() {
return sensor_id;
}
public long getCreatedAt() {
return created_at;
}
public double getValue() {
return value;
}
}
@Repository
public interface RecordsRepository extends CassandraRepository<Record, String> {
@Query("SELECT created_at, value FROM record WHERE sensor_id = ?0 and created_at > ?1")
List<Record.RecordData> findBySensorIdAndCreatedAt(Long sensorId, Long createdAt);
}
堆疊跟蹤:
org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate com.example.mainservice.models.entities.Record using constructor public com.example.mainservice.models.entities.Record(long,long,double) with arguments null,300,0.0
Caused by: java.lang.IllegalArgumentException: Parameter sensor_id must not be null!
uj5u.com熱心網友回復:
嘗試使您的表成為一個物體,以便它檢索資料并將其與您的物件映射。
作為一個應該在資料庫中持久化的類,它必須被注釋使用 javax.persistence 庫進行匯入
用戶 @Id 因為這將使您的 id 成為 JPA 的主鍵
在您的表格中添加@Entity 注釋,如下所示,如果您沒有為表格提供自定義名稱,則無需撰寫@Table,因為@Table 用于命名您的表格。它以該名稱存盤在您的資料庫中
@Entity //insert here
public class Record {
public record RecordData(long created_at, double value) {
}
@Id
private final long sensor_id;
....
}
試試這個它應該作業
uj5u.com熱心網友回復:
你可以試試這個
@Entity
@Table(name="record")
public class Record {
public record RecordData(long created_at, double value) {
}
@Id
private final long sensor_id;
private final long created_at;
private final double value;
public Record(long sensor_id, long created_at, double value) {
this.sensor_id = sensor_id;
this.created_at = created_at;
this.value = value;
}
public long getSensorId() {
return sensor_id;
}
public long getCreatedAt() {
return created_at;
}
public double getValue() {
return value;
}
}
@Repository
public interface RecordsRepository extends CassandraRepository<Record, String> {
@Query("SELECT created_at, value FROM record WHERE sensor_id = :sensor_id and created_at > :created_at")
List<Record.RecordData> findBySensorIdAndCreatedAt(@Param("sensor_id")Long sensorId,@Param("created_at") Long createdAt);
}
uj5u.com熱心網友回復:
在您的代碼中進行以下更改:
@Entity
public class Record {
public record RecordData(long created_at, double value) {
}
@Id
private final long sensor_id;
private final long created_at;
private final double value;
public Record(long sensor_id, long created_at, double value) {
this.sensor_id = sensor_id;
this.created_at = created_at;
this.value = value;
}
public long getSensorId() {
return sensor_id;
}
public long getCreatedAt() {
return created_at;
}
public double getValue() {
return value;
}
}
和
@Repository
public interface RecordsRepository extends CassandraRepository<Record, String> {
@Query("SELECT r.created_at, r.value FROM Record r WHERE r.sensorId = :sensorId and r.createdAt > :createdAt")
List<Record.RecordData> findBySensorIdAndCreatedAt(@Param("sensorId") Long sensorId, @Param("createdAt") Long createdAt);
}
uj5u.com熱心網友回復:
我應該認為有一個單獨的課程會有所幫助
public class RecordData {
private Long createdAt;
private Double value;
public RecordData(Long createdAt, Double value) {
this.createdAt = createdAt;
this.value = value;
}
}
@Repository
public interface RecordsRepository extends CassandraRepository<Record, String> {
@Query("SELECT new RecordData(r.created_at, r.value) FROM Record r WHERE r.sensorId = :sensorId and r.createdAt > :createdAt")
List<RecordData> findBySensorIdAndCreatedAt(@Param("sensorId") Long sensorId, @Param("createdAt") Long createdAt);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/513234.html
