Closed FULaBUla closed 5 months ago
That's not going to work. DTO projections require the use of constructor expressions, see https://docs.spring.io/spring-data/jpa/reference/repositories/projections.html#projection.dynamic.
That's not going to work. DTO projections require the use of constructor expressions, see https://docs.spring.io/spring-data/jpa/reference/repositories/projections.html#projection.dynamic.
Thanks for your tips, I can successfully obtain data using the following methods. The way I use constructors when using dynamic projections still throws exceptions, and I also have to use @Query
because I want to have some hand-written join statements. And if I use the interface class approach, the data I get is empty.
I feel that there are still some problems in this. In particular, I used the @JdbcTypeCode(SqlTypes.JSON)
annotation in the entity.
@Data
public class JsonDataDto implements Serializable {
private int i;
private String a;
public JsonDataDto(ParameterData.Data data) {
this.i = data.i();
this.a = data.a();
}
}
@Query("select new com.example.search.json.JsonDataDto(t1.data) from ParameterData t1")
List<JsonDataDto> getAllData();
The sample code I have provided so far is simplified, but in practice the query statements may be more complex, as shown in the following code. I don't actually want to declare ParameterData.Data data
in the constructor, but I currently have to. I can't get json data directly as a string, nor can I get data as t1.data.id
. Everything except my current construct is reporting errors.
@lombok.Data
public class Data {
private Integer id;
private JsonDataDto data;
public Data(Integer id, ParameterData.Data data) {
this.id = id;
this.data = new JsonDataDto(data);
}
}
@Query("select new com.example.search.json.Data(t1.id, t1.data) from ParameterData t1")
List<Data> getAllData();
@mp911de
TupleBackedMap type is private, so far I have received this error message, I don't know how to deal with it. I would have preferred to get the results directly using
JsonDataDto
. I don't useData
because I want to avoid type conversions elsewhere.Entity:
The class that gets the result: