简体   繁体   English

在hibernate查询执行时,是否将空值赋给基本类型的属性?

[英]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? 虽然你可能得到休眠凝聚NULL0 (尽管可能不是一件容易的全球方式),它是不是真的有效映射你的数据-为什么你会不想失去这样的消息?

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.

相关问题 hibernate异常将空值赋给基本类型setter的属性 - hibernate exception Null value was assigned to a property of primitive type setter 避免“Hibernate异常将Null值分配给基本类型setter的属性”而不使用包装器 - Avoid “Hibernate exception Null value was assigned to a property of primitive type setter” without wrappers null值无法分配给基本类型错误(Spring / Hibernate) - A null value cannot be assigned to a primitive type error (Spring/Hibernate) org.hibernate.PropertyAccessException:空值已分配给布尔类型的属性 - org.hibernate.PropertyAccessException: Null value was assigned to a property of boolean type Hibernate将NULL插入到基本类型字段中 - Hibernate inserting NULL into primitive type field 是否可以为值分配或与值类型变量进行比较? - Can null be assigned to or be compared to a value type variable? org.hibernate.PropertyValueException:非空属性在使用 Spring 引导时引用 null 或瞬态值 - org.hibernate.PropertyValueException: not-null property references a null or transient value while using Spring Boot 尝试发布时出错:org.hibernate.PropertyValueException:非空属性引用 null 或瞬态值 - Getting error while trying to post: org.hibernate.PropertyValueException: not-null property references a null or transient value 分配给基元的对象类型参考变量 - Object type reference variable assigned to a primitive 在实体 class 包装器 class 或 JPA/Hibernate 中的原始类型中选择属性数据类型的最佳实践是哪一个? - Which one is the best practice for choosing data type of a property in entity class Wrapper class or Primitive type in JPA/Hibernate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM