-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to insert 'null' in 'long' fields
PostPosted: Wed Jan 11, 2006 11:46 am 
Newbie

Joined: Wed Jan 11, 2006 11:40 am
Posts: 1
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.TransComponent" table="POS_TRANS_COMPONENT"
dynamic-insert="false"
dynamic-update="false">
<id name="biTransactionId" type="string" column="BI_TRANSACTION_ID" >
<generator class="assigned"/>
</id>
<property name="externalId">
<column name="EXTERNAL_ID"/>
</property>
<property name="externalIdType" >
<column name="EXTERNAL_ID_TYPE"/>
</property>
<property name="activeDate">
<column name="ACTIVE_DATE"/>
</property>
<property name="componentId">
<column name="COMPONENT_ID"/>
</property>
<property name="componentInstanceId">
<column name="COMPONENT_INSTANCE_ID"/>
</property>
<property name="componentInstanceIdServ">
<column name="COMPONENT_INSTANCE_ID_SERV"/>
</property>
<property name="inActiveDate">
<column name="INACTIVE_DT"/>
</property>
<property name="productgroupId">
<column name="PRODUCT_GROUP_ID"/>
</property>
<property name="compTrackingId">
<column name="COMP_TRACKING_ID"/>
</property>
<property name="compTrackingIdServ">
<column name="COMP_TRACKING_ID_SERV"/>
</property>
<property name="packageInstanceId">
<column name="PACKAGE_INSTANCE_ID"/>
</property>
<property name="packageInstanceIdServ">
<column name="PACKAGE_INSTANCE_ID_SERV"/>
</property>
<property name="rate">
<column name="RATE"/>
</property>
<property name="serviceLocationNo">
<column name="SERVICE_LOCATION_NO"/>
</property>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():


Full stack trace of any exception that occurs:

Name and version of the database you are using:Oracle 9i

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Class Code:
package com;
import java.sql.Date;

public class TransComponent {

private String biTransactionId= null;
private String externalId= null;
private int externalIdType;
private Date activeDate= null;
private long componentId;
private long componentInstanceId;
private long componentInstanceIdServ;
private String inActiveDate= null;
private long productgroupId;
private long compTrackingId;
private int compTrackingIdServ;
private long packageInstanceId;
private int packageInstanceIdServ;
private int rate;
private String serviceLocationNo= null;

public Date getActiveDate() {
return activeDate;
}
public void setActiveDate(Date activeDate) {
this.activeDate = activeDate;
}
public String getBiTransactionId() {
return biTransactionId;
}
public void setBiTransactionId(String biTransactionId) {
this.biTransactionId = biTransactionId;
}
public long getComponentId() {
return componentId;
}
public void setComponentId(long componentId) {
this.componentId = componentId;
}
public long getComponentInstanceId() {
return componentInstanceId;
}
public void setComponentInstanceId(long componentInstanceId) {
this.componentInstanceId = componentInstanceId;
}
public long getComponentInstanceIdServ() {
return componentInstanceIdServ;
}
public void setComponentInstanceIdServ(long componentInstanceIdServ) {
this.componentInstanceIdServ = componentInstanceIdServ;
}
public long getCompTrackingId() {
return compTrackingId;
}
public void setCompTrackingId(long compTrackingId) {
this.compTrackingId = compTrackingId;
}
public int getCompTrackingIdServ() {
return compTrackingIdServ;
}
public void setCompTrackingIdServ(int compTrackingIdServ) {
this.compTrackingIdServ = compTrackingIdServ;
}
public String getExternalId() {
return externalId;
}
public void setExternalId(String externalId) {
this.externalId = externalId;
}
public int getExternalIdType() {
return externalIdType;
}
public void setExternalIdType(int externalIdType) {
this.externalIdType = externalIdType;
}
public String getInActiveDate() {
return inActiveDate;
}
public void setInActiveDate(String inactiveDate) {
this.inActiveDate = inactiveDate;
}
public long getPackageInstanceId() {
return packageInstanceId;
}
public void setPackageInstanceId(long packageInstanceId) {
this.packageInstanceId = packageInstanceId;
}
public int getPackageInstanceIdServ() {
return packageInstanceIdServ;
}
public void setPackageInstanceIdServ(int packageInstanceIdServ) {
this.packageInstanceIdServ = packageInstanceIdServ;
}
public long getProductgroupId() {
return productgroupId;
}
public void setProductgroupId(long productgroupId) {
this.productgroupId = productgroupId;
}
public int getRate() {
return rate;
}
public void setRate(int rate) {
this.rate = rate;
}
public String getServiceLocationNo() {
return serviceLocationNo;
}
public void setServiceLocationNo(String serviceLocationNo) {
this.serviceLocationNo = serviceLocationNo;
}

}




In the above mentioned class, I have class level variables with type int and long and when I don't populate the those values before saving that object, the correspoding columns in the table have value 0.

Now how do I avoid inserting the java's default values in the database for example inserting 'null' or nothing in the long fields in the above class.

Thanks in advance.

-Jam


Top
 Profile  
 
 Post subject: Try using Long
PostPosted: Wed Jan 11, 2006 12:01 pm 
Newbie

Joined: Tue Jan 10, 2006 1:30 pm
Posts: 14
When we needed a null in the database where our longs and ints were, we would use the wrapped object Long and Integer.

class Student{
Long graduationDate = null;
}

<property name="graduationDate"
column="GRADUATION_DATE"
type="long"
not-null="false" />

Hope this helps.

Brian

_________________
Brian


Top
 Profile  
 
 Post subject: Re: How to insert 'null' in 'long' fields
PostPosted: Tue Jul 21, 2009 1:48 am 
Newbie

Joined: Tue Jul 21, 2009 1:46 am
Posts: 5
How would I do it the opposite way?

I have a bigint field in my database which may be null
Code:
<property name="organizationId">
            <column name="organization_id" type="long" not-null="false" />
</property>

if it's null I would like the organizationId to be set as 0


Top
 Profile  
 
 Post subject: Re: How to insert 'null' in 'long' fields
PostPosted: Tue Jul 21, 2009 3:14 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Initialize it in your entity class. "protected Long foo = new Long(0);"


Top
 Profile  
 
 Post subject: Re: How to insert 'null' in 'long' fields
PostPosted: Tue Jul 21, 2009 5:05 am 
Newbie

Joined: Tue Jul 21, 2009 1:46 am
Posts: 5
it IS initialized in the entity class...
however, if the database field has a null value, the session factory and transaction give exceptions.... i've temporarily solved it for my purpose, but I was wondering how you would do it....


Top
 Profile  
 
 Post subject: Re: How to insert 'null' in 'long' fields
PostPosted: Tue Jul 21, 2009 5:13 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
I think I understand your problem now... You have a NULL in your database and want 0 on the client. This does not work if you have long instead of Long.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.