Java – hibernate does not get the null value of the field in the database record

I use the following hibernate code to get data from the database

SessionFactory factory = null;
    Session session = null;
    try {
        factory = getSessionFactory();
        session = factory.openSession();

        final Criteria criteria = session
                .createCriteria(CrfEmailDataBean.class);
        criteria.add(Restrictions.eq(CAMPN_NBR,campNbr));
        returnList = criteria.list();
    } catch (Exception e) {
        logger.error(e.getMessage());
        throw new DAOException(e);
    } finally {
        DBUtil.close(factory,session);
    }
    if (logger.isInfoEnabled()) {
        logger.info(LOG_METHOD_EXIT);
    }
    return returnList;
}

In the crfemaildatabean class, I have declared a private string crfemailtypecd; Null field in database Because it is null, it does not set records in the return list If I enter a value into a field in the database, it will get it

I try to run the query directly on the SQL database. The query is correct and get all the data

Why not extract the record? How can I solve this problem?

Solution

When you use the same type attribute primitive, you need to set the default value, the same zero (0) or (a) (..)

private int number;
private int value;
private char option;

If the hibernate database is ready, when the converter of Hibernate generator fails

>This solution that never the user's original type > or define a default value for the attribute > every other possibility your date is not empty, this value is white > White is different from null > make test condition restrictions isNotNull. > Make other test conditions restrictions isNull. > Other tests make a method receive int and call send an integer null

First item

private Integer number;
private Integer  value;
private Character option;

Last test

public class Test{

    public static void setInt(int value){
       System.out.println(value);
    }

    public static void main(String[] args)
    {
        Integer testValue=null;
        Test.setInt(testValue);
    } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>