0

I have an application that is connected to an SQL database using Hibernate. When removing a person from a particular part of the application I want to reset all the information that had been set in the particular part. Below is an example of what I am on about

Stage: Bank

Fields to be set in stage:
BankName
AccNo
IBANNo
...

When removing a Person from this stage, what is the best way to reset all the information

public void resetInfo(Person person){
    person.setBankName(null);
    person.setAccNo(null);
    person.setIBANNo(null;)
}

or

public void resetInfo(Person person){
    if(person.getBankName != null)  {  
        person.setBankName(null);
    }
    if(person.getAccNo != null)  {  
        person.setAccNo(null);
    }
    if(person.getIBANNo != null)  {  
        person.setIBANNo(null;)
    }
}

or is there a better way to do this?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Hip Hip Array
  • 4,665
  • 11
  • 49
  • 80

4 Answers4

3

Your former code fragment looks better and more readable.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Azodious
  • 13,752
  • 1
  • 36
  • 71
2
public void resetInfo(Person person) {     
    person.setBankName(null);     
    person.setAccNo(null);     
    person.setIBANNo(null;) 
} 

This is fine, as Hiberante internally keeps track of which all field values are changed to specify that in update query.

Also you can check this by setting show_sql = true, to check what update query is generated by Hibernate.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Rahul Agrawal
  • 8,913
  • 18
  • 47
  • 59
1

Are you using JPA/Hibernate/Other ORM? If so - just set your fields to null and let the framework create the query it needs (meaning - if the fields were null before nothing will be run).

If you use JDBC - collect all changes and run a single update.

As for the object itself, it doesn't matter if you check the value before you set it to null. You can just use your first example (call setNull() on all fields).

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Zach Moshe
  • 2,782
  • 4
  • 24
  • 40
  • I am using Hibernate... Does this mean I do not need to check if they are already null as nothing will be run if they are? – Hip Hip Array Oct 22 '12 at 10:30
  • exactly. If the object you try to persist is equal to the one that hibernate got from the DB - it won't update. So if all fields were null and you set them again to null you haven't changed the object and nothing will happen. – Zach Moshe Oct 22 '12 at 13:40
1

Having three bank fields in a Person class doesn't smell good. What about moving those fields to a specific Bank class? Then you could do setBank(null), to clear the bank information.

If you still would like your bank information to be in the same row as the person information, you could use @Embedded in JPA.

Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
  • In my actually code I have an actual Bank object which has all this information, just did it this way as an example (in my code it is person.getBank.setWhatever)... But i am also using tapestry and if i use setBank(null) I get a NullPointerException – Hip Hip Array Oct 22 '12 at 10:37