1

i have this in my repository

@Transactional
@Modifying
@Query(value = "UPDATE pengajuan u set u.status_approval ='Komite' where u.id_pengajuan =:idPengajuan",
        nativeQuery = true)
void updateStatusPengajuan(@Param("statusApproval") String statusApproval, @Param("idPengajuan") Integer idPengajuan);

i want to set status_approval to 'Komite' by 'idPengajuan'

now i have this in my services

    public PengajuanK3 update(String statusApproval,int idPengajuan){
    return pengajuanK3Repository.updateStatusPengajuan(statusApproval, idPengajuan);
}

im littlebit confuse how i can call the repository in services because in repository is void type.

Ernesto
  • 39
  • 6
  • Well, you change the return type to void, and you remove the `return`. Note that your repository method is wrong: it ignores one of the parameters. – JB Nizet Dec 19 '19 at 07:53
  • @JBNizet is the query right? for harcoded? or i need to add something in my controller? – Ernesto Dec 19 '19 at 08:15
  • @YohanesEAC why are you sending statusApproval as a parameter in the updateStatusPengajuan method? – Suraj Gautam Dec 19 '19 at 08:37

1 Answers1

1

The query that you have used always sets status_approval to Komite. In this sense, you don't need to pass the parameter in your repository update method. Everything is fine.

But if you want to update status_approval dynamically from the parameter other than 'Komite' then do like this:

Repository:

@Transactional
@Modifying
@Query(value = "UPDATE pengajuan u set u.status_approval =:statusApproval where u.id_pengajuan =:idPengajuan",
    nativeQuery = true)
void updateStatusPengajuan(@Param("statusApproval") String statusApproval, 
@Param("idPengajuan") Integer idPengajuan);

And in your service change the return type to void and remove return.

 public void update(String statusApproval,int idPengajuan){
     pengajuanK3Repository.updateStatusPengajuan(statusApproval,idPengajuan);
}

In your controller, call the update method like:

service.update('Komite', 1);

I prefer this way rather than hardcoding because in the future if you need to set status_approval to other values, you can do it by:

service.update('othervalues', 1);

Suraj Gautam
  • 1,524
  • 12
  • 21