Reputation: 4753
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?
Upvotes: 0
Views: 736
Reputation: 8971
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.
Upvotes: 2
Reputation: 2980
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).
Upvotes: 1
Reputation: 18542
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.
Upvotes: 1