0

So I'm making an API using Java Spring-boot and JBDC with PostgreSQL as the database and Postman for testing. All the basic CRUD function is already implemented, but I'm trying to validates if a value in the POST request is already in the database or not so there are no duplicate data.

Here's the model:

package com.akuntansi.akuntansi.model;

import java.sql.Timestamp;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;

public class mAkuntansi {
    private Integer id_md_jurnal_Integer;
    private Integer id_dd_wilayah_kerja;
    private String kode_file;
    private String file_id;
    private String tanggal;
    private String debet_kredit;
    private String kode_coa;
    private long nominal;
    private Integer flag;
    private Integer id_akmt_subledger;
    private Integer id_referensi;
    private String no_bukti;
    private Timestamp created_date;

    public mAkuntansi(){}

    public mAkuntansi(Integer id_md_jurnal_Integer, Integer id_dd_wilayah_kerja, String kode_file, String file_id, String tanggal, String debet_kredit, String kode_coa, 
    long nominal, Integer flag, Integer id_akmt_subledger, Integer id_referensi, String no_bukti, Timestamp created_date){
        this.id_md_jurnal_Integer = id_md_jurnal_Integer;
        this.id_dd_wilayah_kerja = id_dd_wilayah_kerja;
        this.kode_file = kode_file;
        this.file_id = file_id;
        this.tanggal = tanggal;
        this.debet_kredit = debet_kredit;
        this.kode_coa = kode_coa;
        this.nominal = nominal;
        this.flag = flag;
        this.id_akmt_subledger = id_akmt_subledger;
        this.id_referensi = id_referensi;
        this.no_bukti = no_bukti;
        this.created_date = created_date;
    }

    public mAkuntansi(Integer id_dd_wilayah_kerja, String kode_file, String file_id, String tanggal, String debet_kredit, String kode_coa, 
    long nominal, Integer flag, Integer id_akmt_subledger, Integer id_referensi, String no_bukti, Timestamp created_date){
        this.id_dd_wilayah_kerja = id_dd_wilayah_kerja;
        this.kode_file = kode_file;
        this.file_id = file_id;
        this.tanggal = tanggal;
        this.debet_kredit = debet_kredit;
        this.kode_coa = kode_coa;
        this.nominal = nominal;
        this.flag = flag;
        this.id_akmt_subledger = id_akmt_subledger;
        this.id_referensi = id_referensi;
        this.no_bukti = no_bukti;
        this.created_date = created_date;
    }
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getid_md_jurnal_int(){
        return id_md_jurnal_Integer;
    }

    public void setid_md_jurnal_int(Integer id_md_jurnal_Integer){
        this.id_md_jurnal_Integer = id_md_jurnal_Integer;
    }

    public Integer getid_dd_wilayah_kerja(){
        return id_dd_wilayah_kerja;
    }

    public void setid_dd_wilayah_kerja(Integer id_dd_wilayah_kerja){
        this.id_dd_wilayah_kerja = id_dd_wilayah_kerja;
    }

    public String getkode_file(){
        return kode_file;
    }

    public void setkode_file(String kode_file){
        this.kode_file = kode_file;
    }

    public String getfile_id(){
        return file_id;
    }

    public void setfile_id(String file_id){
        this.file_id = file_id;
    }

    public String gettanggal(){
        return tanggal;
    }

    public void settanggal(String tanggal){
        this.tanggal = tanggal;
    }

    public String getdebet_kredit(){
        return debet_kredit;
    }

    public void setdebet_kredit(String debet_kredit){
        this.debet_kredit = debet_kredit;
    }

    public String getkode_coa(){
        return kode_coa;
    }

    public void setkode_coa(String kode_coa){
        this.kode_coa = kode_coa;
    }

    public long getnominal(){
        return nominal;
    }

    public void setnominal(long nominal){
        this.nominal = nominal;
    }

    public Integer getflag(){
        return flag;
    }

    public void setflag(Integer flag){
        this.flag = flag;
    }

    public Integer getid_akmt_subledger(){
        return id_akmt_subledger;
    }

    public void setid_akmt_subledger(Integer id_akmt_subledger){
        this.id_akmt_subledger = id_akmt_subledger;
    }

    public Integer getid_referensi(){
        return id_referensi;
    }

    public void setid_referensi(Integer id_refrensi){
        this.id_referensi = id_refrensi;
    }

    public String getno_bukti(){
        return no_bukti;
    }

    public void setno_bukti(String no_bukti){
        this.no_bukti = no_bukti;
    }

    public Timestamp getcreated_date(){
        return created_date;
    }

    public void setcreated_date(Timestamp created_date){
        this.created_date = created_date;
    }

}

Let say for example I want to validates the kode_coa. There's a POST request with kode_coa of K1 but in the database, a data with value K1 already exist, how do you prevent a duplicate data from getting inserted into the database?

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
  • That highly depends on your requirements. If each value of `kode_coa` must every exist only once in the DB you should use a unique constraint. If that only applies to testing you might use a query to check if it returns something for the value you're looking for. – Thomas Feb 09 '23 at 07:54
  • 1
    Create a unique constraint on the table (-s) involved. Your application can't see uncommitted data and thus it will miss all data that has not been committed yet. – Frank Heikens Feb 09 '23 at 07:56
  • Btw, that code looks problematic since it doesn't follow the Java beans conventions which state the getter for `nominal` should be `getNominal()` instead of `getnominal()`. Many frameworks use this and if your getters look different they might run into problems. Also, you should do yourself a favor and read up and stick to the Java code conventions, e.g. class names should start with an uppercase character. You might also look up some best practices, e.g. those published by Google. – Thomas Feb 09 '23 at 07:57
  • See this [https://stackoverflow.com/a/469553/12715723](https://stackoverflow.com/a/469553/12715723) to set unique value on spesific column. – Jordy Feb 09 '23 at 07:58
  • This is actually an API that was converted from PHP to Java, so I need to follow the naming. Anyway what if I just want to make a function that return a value of true or false depending if the data already exist inside the database or not since I need to return an error message if the data already exist to the user – Timothy Justin William Feb 09 '23 at 08:12
  • This is not the correct approach - [if a value in the POST request is already in the database] All http requests work async. For example, you can convert all post methods to sync for waiting results. Result may be succussing or failing. But duplicates are controlled by unique indexes on the DB side. – Ramin Faracov Feb 09 '23 at 08:30

0 Answers0