I'm working on a spring boot project, there I have two tables that are related to each other with OneToMany relationship
public class PackGroupEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String description;
private Double qty;
private Integer packingNr;
@Temporal(TemporalType.TIMESTAMP)
private Date deliveredTime;
@OneToMany(mappedBy = "packGroup", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private List<PackArticlesEntity> packArticles= new ArrayList<>();
}
And
public class PackArticlesEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Double qty;
private Double confirmedName;
@Enumerated(EnumType.STRING)
private ArticleStatus status;
private Double weight;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "pack_group_id")
private PackGroupEntity packGroup;
}
And I insert data on these two tables in a loop, first I group the articles by packing number for which I will create a PackGroup that has a list of articles:
Map<Integer, List<RecivedArticlesDTO >> groupedArticles =
recivedArticlesListDTO.stream().collect(Collectors.groupingBy(RecivedArticlesDTO::getPackingNr));
for (Map.Entry<Integer, List<RecivedArticlesDTO>> entry : groupedArticles.entrySet()) {
List<RecivedArticlesDTO > groups = entry.getValue();
PackGroupEntity packGroup = new PackGroupEntity();
packGroup.setPackingNr(entry.getKey())
//some manipulations and setting data for each field
List<PackArticlesEntity> listWithArticles = new ArrayList<>();
groups.forEach(pack -> {
PackArticlesEntity packArticle= new PackArticlesEntity();
packArticles.setQty(pack.getQty);
//some manipulations and setting data for each field
listWithArticles.add(packArticles);
}
packGroup.setPackArticles(listWithArticles);
// here I have to save the data using native query
packGroupService.savePackGroupData(packGroup);
}
In this way, it is so slow so I wanted to do it on the native query.
The problem is that I have many packGroups a many packArticles that needs to be saved. I was thinking to somehow do only one connection with DB to send the list of pack groups and pack articles to save, but I don't know how to do this on native queries. This here is just for one pack_group but yet I don't know how to pass packArticles
on a native query since it's a list
@Query(value = "insert into pack_group " +
" (id,packing_nr, description, qty, deliveredTime, packArticles) " +
" values (1?, 2?, 3?, 4?, 5?, 6?)", nativeQuery = true)
void savePackGroupData(id, packing_nr, description, qty, packArticles);
Can someone help with this, please?
EDIT: I want to return the id from insert on
String query = String.format("insert into pack_group(group, remark, description ) " +
"values ( %s, %s, %s)", "x","y","z" );
Query q = entityManager.createNativeQuery(query );
BigInteger biid = (BigInteger) q.getSingleResult();
long id = biid.longValue();
And I get this error com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.