Получить массив сущностей из jsonb объекта
Мой запрос, который по моему мнению должен возвращать список сущностей
для дальнейшей фильтрации по DocumentType
@Repository
public interface SchematronRepository extends JpaRepository<SchematronEntity, String>{
@Query(value = "select \"SCHEMATRONS\" from \"SEMD\".\"SEMD_VALIDATION_SCHEMA\" where \"IMPLEMENTATION_MANUAL_OID\" = :oid", nativeQuery = true)
ArrayList<SchematronJson> getContentByImplementationManualOid(String oid);
}
Что я получаю в итоге
No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [DocumentValidator.Domain.SchematronJson]
Моя сущность, которую я хочу видеть как результат запроса
public class SchematronJson implements Serializable {
public SchematronJson(String Name, String Content, int DocumentType){
this.Name = Name;
this.Content = Content;
this.DocumentType = DocumentType;
}
private String Name;
private String Content;
private int DocumentType;
public String getName() {
return Name;
}
public String getContent() {
return Content;
}
public int getDocumentType() {
return DocumentType;
}
}
То что хранится в колонке SCHEMATRONS имеет вид
[
{
"Name" : "name1",
"Content" : "content1",
"DocumentType" : 1
},
{
"Name" : "name2",
"Content" : "content2",
"DocumentType" : 2
}
]
Я новичок в Java, не совсем понимаю как маппить результат запроса в сущность сразу. Подскажите пожалуйста что я должен сделать чтобы это работало?
Ответы (1 шт):
Автор решения: Ivanushka
→ Ссылка
Что-ж, мне удалось решить эту проблему таким образом.
Класс SchematronJson теперь выглядит так.
public class SchematronJson implements Serializable {
public SchematronJson(){ }
private String Name;
private String Content;
private int DocumentType;
public String getName() {
return Name;
}
public void setName(String Name){
this.Name = Name;
}
public String getContent() {
return Content;
}
public void setContent(String Content){
this.Content = Content;
}
public int getDocumentType() {
return DocumentType;
}
public void setDocumentType(String DocumentType){
this.DocumentType = Integer.parseInt(DocumentType);
}
Я поменял в нем конструктор и добавил сеттеры.
Так же изменениям подвергся запрос
public String getByImplementationManualOid(String implementationManualOid, String documentType) {
String sql = "select \"SCHEMATRONS\" from \"SEMD\".\"SEMD_VALIDATION_SCHEMA\" where \"IMPLEMENTATION_MANUAL_OID\" = ?";
var schematrons = jdbcTemplate.query(sql,(rs, rowNum) -> {
String json = rs.getString("SCHEMATRONS");
JsonNode jsonNode;
try {
jsonNode = objectMapper.readTree(json);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
var schList = new ArrayList<SchematronJson>();
jsonNode.forEach(x -> {
var schematron = new SchematronJson();
schematron.setName(x.get("Name").asText());
schematron.setContent(x.get("Content").asText());
schematron.setDocumentType(x.get("DocumentType").asText());
schList.add(schematron);
});
return schList.stream().filter(x -> x.getDocumentType() == Integer.parseInt(documentType)).findFirst().orElse(new SchematronJson());
}, implementationManualOid);
return schematrons.stream().findFirst().get().getContent();
}