I am trying to return a custom object RecordData from my repository class, but it tries to instantiate my main Record class. I've tried using Interface Projections, but the result is the same.
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);
}
Stack-trace:
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!
Make these changes in your code:
And