2

I have the following problem:

We are writing a web application using Hibernate and Struts. Upon submit, I want to check, if a DB update has be executed or not. So I pull the Object out of the DB and compare it to the data in the form (let's assume, I have a Person object with some attributes like Name, Address, etc.).

Now if the field Address on the web form is empty the form property would give me an "" (Empty String). If I take the Address property which I got from the DB object and compare it to the form property, the comparison would return FALSE because I compare an "" (Empty String) with NULL which obviousley is not the same.

I tried the following methods:

if (StringUtils.equals(formObj.getAddress(), dbObj.getAddress())) {
        dbObj.getAddress(formObj.getAddress());
        dbObj.update();
}
if (ObjectUtils.equals(formObj.getAddress(), dbObj.getAddress())) {
        dbObj.getAddress(formObj.getAddress());
        dbObj.update();
}

My question is: Is there a method that could take these to String Values and compares them, whereby "" == NULL would return TRUE?

4 Answers 4

2

No, because they are not the same. "" is very different from null.

Now if you want this behavior write a custom method:

static boolean checkIfEmptyOrNull(String s1, String s2) {
     //check for empty and null
     return myBooleanValue;
}

Mark this as static because this should be a helper (and should probably go in a helper class as well)

Sign up to request clarification or add additional context in comments.

4 Comments

@piet.t - They had already created DBs before they bought Sun :P
That's what we thought too. Oracle is the first DB I know, that handles "" as NULL... I'd like to know why :P
@rGunti - Maybe because at a low level "" and null makes no difference and they both represnt "nothing"
@piet.t They new it as of 8.0, but not sure if this comply to SQL standards.
1

As oracle stores all empty strings as null, we have been mapping the respective columns with a custom data-type that maps all null-values in the database to empty strings. So all Strings read from the database will be empty instead of null.

To create this type you implement a class extending org.hibernate.usertype.UserType;:

import org.hibernate.usertype.UserType;
public class OracleStringType implements UserType {...}

Most required method-implementations are quite straightforward, as you just return the parameter passed in. The most interesting are these:

@Override
public boolean equals(Object arg0, Object arg1) throws HibernateException {
    if(arg0 == null) arg0 = "";
    if(arg1 == null) arg1 = "";
    return arg0.equals(arg1);
}
@Override
public int hashCode(Object arg0) throws HibernateException {
    if(arg0==null) arg0="";
    return arg0.hashCode();
}
@Override
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
        throws HibernateException, SQLException {
    String result = resultSet.getString(names[0]);
    if(resultSet.wasNull()) return "";
    return result;
}

@Override
public void nullSafeSet(PreparedStatement statement, Object value, int index)
        throws HibernateException, SQLException {
    if(value == null) {
        statement.setNull(index, Hibernate.STRING.sqlType());
    } else {
        String valueString = (String)value;
        statement.setString(index, valueString);
    }
}

1 Comment

We thought of this ourself too but since we had to change a lot of our Hibernate classes for this, we discarded this approch. Another problem we thought of, if we'd use another DB (other than Oracle), this behavour would become unhandy.
1

I will leave my solution here:

I added a method in our own StringUtils class (we call it StringTools to avoid conflicts with Apache's StringUtils) for this purpose:

public static boolean notEqualHibernateProperty(String s1, String s2) {
     return !ObjectUtils.equals(StringUtils.defaultString(s1), StringUtils.defaultString(s2));
}

StringUtils.defaultString(s) returns an empty String, if the parameters value is NULL. Then I simply changed the update condition to:

if (StringTools.notEqualHibernateProperty(formObj.getAddress(), dbObj.getAddress())) {
     dbObj.getAddress(formObj.getAddress());
     dbObj.update();
}

Comments

0

use this additional condition:

if (StringUtils.isEmpty(formObj.getAddress()) && StringUtils.isEmpty( dbObj.getAddress()))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.