0

i have a code, like this

public static Response updateDataFiles(String id, String checksum){
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();
    Transaction trans = session.beginTransaction();

    Files files = (Files) session.get(Files.class, id);
    System.out.println("checksum "+checksum);
    System.out.println("checksum file "+files.getChecksumFile());
    String checksumFile = files.getChecksumFile();
    if(checksum == checksumFile){
        System.out.println("upload success");           
        files.setStatusUpload(EnumStatusUpload.statusUpload.UPLOADED_AND_SUCCESS.toString());                                   
    }else{
        System.out.println("upload success but checksum error");            
        files.setStatusUpload(EnumStatusUpload.statusUpload.UPLOADED_BUT_ERROR_CHECKSUM.toString());
    }
    session.update(files);
    trans.commit();
    session.flush();
    session.close();

    Response respon = new Response();
    respon.status = 200;
    return respon;
}

at if(checksum == checksumFile) there's something wrong, the checksum variable and checksumFile have a same value, but if did not execute the right condition, if executed else condition. In my log i had seen the value of checksum variable, and checksumFile like this

checksum      9d73d945294d5488056bb5da54f62e8f
checksum file 9d73d945294d5488056bb5da54f62e8f

i don't know what's wrong in my code. Can anyone help me? and sorry for my bad English

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Yusuf1494
  • 252
  • 1
  • 5
  • 16

1 Answers1

6

You want to compare the values of the two strings using .equals()

checksum.equals(checksumFile)

Using == compares the references and basically asks whether the two references point to the same object, which they don't.

go-oleg
  • 19,272
  • 3
  • 43
  • 44