-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: How to persist Null value to primitive value?
PostPosted: Wed Feb 11, 2004 12:22 am 
Newbie

Joined: Tue Feb 10, 2004 11:15 pm
Posts: 7
dear all,


I have a question please help me.

suppose I have file mapping SysConfig.hbm.xml like :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="appserver.domain.SysConfig" table="SYSCONFIG">
<id column="PARAMID" name="id" type="string">
<generator class="assigned"/>
</id>
<property column="PARAMVALUE" length="7" name="parameterValue" type="integer"/>
</class>
</hibernate-mapping>

-------- and I have file mapping SysConfig.java like ;

public class SysConfig implements Serializable {

/** identifier field */
private String id;

/** nullable persistent field */
private int parameterValue;

/** full constructor */
public SysConfig(java.lang.String id, int parameterValue) {
this.id = id;
this.parameterValue = parameterValue;
}

/** default constructor */
public SysConfig() {
}

/** minimal constructor */
public SysConfig(java.lang.String id) {
this.id = id;
}

/--------- Getters and Setters are deleted ---------------/
}


As you can see the "parameterValue" are map to java primitive (int) rather than Object Integer. The problem arose when I use :

hs.find("SELECT s FROM SysConfig s");

and the database table are like this

Table SYSCONFIG
---------------------------------------------------------------------
PARAMID | PARAMVALUE
--------------------------------------------------------------------
1 | <NULL>



I think the problem exist because Hibernate "cannot" persist variable "parameterValue" (primitive int) to be Null.

because doing

setParameterValue(Null)

would throws exception because the expected input value is (primitive) int not Object Integer.


What I like to ask you all is how can I make the Null value become 0 before Hibernate set the Null value into "parameterValue"? Or can you would propose any other solution?

The constraint are :
1. I am forbiden to change any database settings at all.
2. I do not want to change primitive value int to be Object Integer.


The preferable change to correct the problem :
1. Through Hibernate Settings.
2. Any

Please give me step-to-step instruction to correct my problem?


thank you very much for your time and consideration.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 2:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so - how do you expect hibernate to map null to a value in the integer when loading the object ?

If you have a "common" rule (like -1 == null) then you should try to make a UserType....or just hava get/set's for the Integer which transform it to a primitie.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 3:30 am 
Newbie

Joined: Tue Feb 10, 2004 11:15 pm
Posts: 7
I was thinking in the hibernate configuration can be set so that Hibernate would change the Null value into 0 before the Null value is persisted (using the setter).

If this kind of configuration does not exist, OK then i'll just change all database mappings to be Object instead of primitive.

Thank you very much for your Information.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 4:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
may I ask what you will do if the value actually is 0 ? ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 6:40 am 
Newbie

Joined: Tue Feb 10, 2004 11:15 pm
Posts: 7
if the value becomes 0 instead of Null then the value from the database can be persist to primitive int because persisting Null to primitive int cause Exception.


~plz pardon my english


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.