[英]Null value was assigned to a property of primitive type while hibernate query execution?
i have below code snippet in dao 我在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
Customer table has some int columns for which some values as null. Customer表有一些int列,其中一些值为null。 Now at line1 i get error Null value was assigned to a property of primitive type setter of Customer.Address
现在在line1我得到错误Null值被分配给Customer.Address的原始类型setter的属性
Is there a way in hibernate/query/Transformer to convert the null values to 0 automatically? 在hibernate / query / Transformer中有没有办法自动将空值转换为0? I have just mentioned one table ie customer but there are various joined table which contains int value as null in various columns so i do not want to handle in query for each table.
我刚刚提到了一个表即客户,但是有各种连接表,其中包含各种列中的int值为null,因此我不想在每个表的查询中处理。
UPDATE:- Customer is not Hibernate entity. 更新: -客户不是Hibernate实体。 It pojo with instance fields of int type
它与int类型的实例字段pojo
Customer table has some int columns for which some values as null.
Customer表有一些int列,其中一些值为null。
In that case I'd recommend making them Integer
fields instead. 在这种情况下,我建议改为使用
Integer
字段。 While you could probably get Hibernate to coalesce NULL
to 0
(although possibly not in an easy global manner), it's not really mapping your data effectively - why would you want to lose information like that? 虽然你很可能得到休眠凝聚
NULL
到0
(尽管可能不是一件容易的全球方式),它是不是真的有效映射你的数据-为什么你会不想失去这样的消息?
I don't know about Hibernate transformers, but I have a different idea. 我不知道Hibernate变换器,但我有一个不同的想法。 Can you change the database?
你能改变数据库吗? You could make the column not nullable and with default value 0. That way all possible values you can store in the database can be mapped to the int primitive.
您可以使列不可为空并使用默认值0.这样,您可以存储在数据库中的所有可能值都可以映射到int原语。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.