6

I am using the below hibernate code to fetch the data from 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;
}

Inside CrfEmailDataBean class, I have declared a private String crfEmailTypeCd; field which is null in database. Because of null, it is not setting the record in return list. If I go and enter a value inside the field in database, it fetches.

I tried running the query directly on sql database, the query formed is correct and fetches all the data.

Why it is not fetching that record? and how can I resolve this?

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
themaster
  • 453
  • 11
  • 32
  • Post the CrfEmailDataBean class as well – StanislavL Aug 12 '16 at 17:36
  • @StanislavL Its a normal data bean having getters and setters . – themaster Aug 13 '16 at 03:06
  • What's the real issue here? Is your query not working as expected, or saving of `CrfEmailDataBean` instances is not working correctly? – Dragan Bozanovic Aug 15 '16 at 13:22
  • 5
    Not sure why people are upvoting this. Without the entity mapping nobody will be able to answer this question. – Jan-Willem Gmelig Meyling Aug 16 '16 at 20:31
  • 1
    Hi all , I had recieved an answer which I am not able to see now . Based on his suggestion , I checked my hbm file . I had included the said field as composite-id in my hbm file . Now it is working .Thanks – themaster Aug 17 '16 at 07:11
  • Please add your full informations to the topic, like entity mapping. Also the solution should be posted as an answer. – Hash Aug 18 '16 at 13:15
  • http://stackoverflow.com/questions/29383553/how-to-handle-database-null-values-in-hibernate-application – omkar sirra Aug 18 '16 at 14:07
  • 1
    What about `CAMPN_NBR`, `campNbr`, `getSessionFactory()`, `CrfEmailDataBean.class`? Provide the code, please. Which version of Hibernate do you use? – Rudziankoŭ Aug 18 '16 at 14:59
  • 3
    You should post your answer instead of adding it to question. Then it will be easier to other to find it. – talex Aug 18 '16 at 15:18

1 Answers1

0

when you use type properties primitive same, need set the default value, same zero (0) or (a) ( ..)

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

if hibernate database ready null, when converters of the Hibernate generator a error

  • solution for this never user primitive type
  • Or define de default value for properties
  • Each other possibility your date not null, this value white
  • white is different of the null
  • make a test put conditions Restrictions.isNotNull.
  • make other test put conditions Restrictions.isNull.
  • other test make a method receive int, call send one Integer null

first item

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

last item 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);
    } }
Marcelo Ferreira
  • 428
  • 1
  • 5
  • 20