i need to adjust the result of findall result jpa repository. Here is the entity
<code>public class AppComponent {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
@Column(name = "sequence")
private int sequence;
@OneToMany(
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "appComponent"
)
@JsonManagedReference
private List<AppElement> appElements;
}
</code>
<code>public class AppComponent {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
@Column(name = "sequence")
private int sequence;
@OneToMany(
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "appComponent"
)
@JsonManagedReference
private List<AppElement> appElements;
}
</code>
public class AppComponent {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
@Column(name = "sequence")
private int sequence;
@OneToMany(
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "appComponent"
)
@JsonManagedReference
private List<AppElement> appElements;
}
i am using stream to adjust the structure, the code is working. the problem is, i am hard coding the key of the map. how to get the field name, so i dont need to write it like that? or are there any better ways to do it?
<code>appComponentGroup.getAppComponents().stream()
.map(
appComponent -> Map.of(
"sequence",
appComponent.getSequence(),
"elements",
appComponent.getAppElements().stream().collect(
Collectors.toMap(
appElement -> appElement.getName(),
appElement -> appElement.getValue()
)
)
)
)
</code>
<code>appComponentGroup.getAppComponents().stream()
.map(
appComponent -> Map.of(
"sequence",
appComponent.getSequence(),
"elements",
appComponent.getAppElements().stream().collect(
Collectors.toMap(
appElement -> appElement.getName(),
appElement -> appElement.getValue()
)
)
)
)
</code>
appComponentGroup.getAppComponents().stream()
.map(
appComponent -> Map.of(
"sequence",
appComponent.getSequence(),
"elements",
appComponent.getAppElements().stream().collect(
Collectors.toMap(
appElement -> appElement.getName(),
appElement -> appElement.getValue()
)
)
)
)
the output looks like this
<code>"review": [{
"sequence": 1,
"elements": {
"reviewer": "john doe",
"rating": 5
}
}]
</code>
<code>"review": [{
"sequence": 1,
"elements": {
"reviewer": "john doe",
"rating": 5
}
}]
</code>
"review": [{
"sequence": 1,
"elements": {
"reviewer": "john doe",
"rating": 5
}
}]