So I am writing native queries because they are very complex to get all the needed data and so on. I am currently facing a problem which is normally done by Hibernate/JPA etc.
Imagine the following:
@Entity
FooEntity{
@Id
public Long id;
@ManyToMany
public List<FeeEntity> feeEntities;
}
@Entity
FeeEntity{
@Id
public Long id;
@Column
public String name;
}
And some DTO
FooDTO{
private final Long id;
private final List<FeeDTO> someStrings;
public FooDTO(Long id, List<FeeDTO> feeDtos){
...
}
}
My @SqlResultSetMapping
looks basically like
@SqlResultSetMapping(name = "FooDTO",
classes = @ConstructorResult(targetClass = FooDTO.class, columns = {
@ColumnResult(name = "id", type = Long.class),
//TODO @ColumnResult(name = "feeDtos", type = FeeDtos.class)
})
)
The named native query looks something like:
@NamedNativeQueries({
@NamedNativeQuery(
name = "FooData",
query = "SELECT MAINSELECT.ID AS id, " +
"???" +
" FROM Foo MAINSELECT WHERE ... " +
...,
resultSetMapping = "FooDTO")
})
How do I have to write the native query? Is this even possible without a subquery or do I have to do/execute a subquery for each datarow of the result? I was not able to find something on beloved google.
That's not possible.
The result of SQL query is always a table. So there are no nested tables and hence you cannot map it to a Collection.