Was null assigned to primitive type property when doing hibernation request?

I have a piece of code in dao

 String GET_CUSTOMER="SELECT * from customer where custName=:custName"
  Session session = getHibernateUtil().getSession();
  SQLQuery query = session.createSQLQuery(GET_CUSTOMER);

    query.setParameter("custName", custName);
    query.setResultTransformer(Transformers
        .aliasToBean(Customer.class));

    Customer custData = (Customer) query
        .uniqueResult();//line

      

The Customer table has multiple int columns for which some of the values ​​are zero. Now on line1 I am getting the error Zero was assigned to primitive setter property Customer.Address

Is there a way in hibernate / query / Transformer to automatically convert null values ​​to 0? I just mentioned one ie table, but there are various concatenated tables that contain int as null in different columns, so I don't want to handle the query for each table.

UPDATE: - Client is not a Hibernate object. This is a pojo with instance fields of type int

+3


source to share


2 answers


The Customer table has multiple int columns for which some of the values ​​are zero.



In this case, I would recommend creating Integer

fields instead . While you could probably merge Hibernate to merge NULL

in 0

(although perhaps not in a simple global manner), it doesn't really map your data efficiently - why would you want to lose information like that?

+5


source


I don't know about Hibernate transformers, but I have a different idea. Can you change the database? You can invalidate a column with a default value of 0. Thus, all possible values ​​that you can store in the database can be mapped to the int primitive.



0


source







All Articles