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!
CodePudding user response:
Try making your table an entity so that it retrieves the data and map it with your object.
As a class which should be persisted in a database it must be annotated Use javax.persistence library for imports
User @Id as this will make your id a primary key as per JPA
Add the @Entity annotation in your table like below and there is no need to write @Table if you are not giving a custom name to your table as @Table is used to name your table. It is stored as that name in your database
@Entity //insert here
public class Record {
public record RecordData(long created_at, double value) {
}
@Id
private final long sensor_id;
....
}
Try this it should work
CodePudding user response:
I should think having a separate class would help
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, value) FROM Record r WHERE r.sensorId = :sensorId and r.createdAt > :createdAt")
List<RecordData> findBySensorIdAndCreatedAt(@Param("sensorId") Long sensorId, @Param("createdAt") Long createdAt);
}
CodePudding user response:
you can try this
@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);
}
CodePudding user response:
Make these changes in your code:
@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;
}
}
And
@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);
}