*
The moose likes Object Relational Mapping and the fly likes Hibernate: Save to Database throwing exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: Save to Database throwing exception" Watch "Hibernate: Save to Database throwing exception" New topic
Author

Hibernate: Save to Database throwing exception

Namrita Mohanty
Ranch Hand

Joined: Sep 26, 2010
Posts: 35
Hi,

I wrote a spring program to save a record to a database using hibernate.
its giving me any error:

"010-10-07 09:34:26,372 ERROR [http-8080-3] com.ing.workstation.resource.appcapture.inquiry.daoimpl.CustomerDAOImpl:113 - save failed
org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.ing.workstation.resource.appcapture.dto.Customer.otherIDNumber; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.ing.workstation.resource.appcapture.dto.Customer.otherIDNumber
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:645"


Code Used:

public Customer save(Customer customer) {
log.debug("saving NBCustomer instance");
try {
String custNumber = customer.getCustomerNumber();
// set customerNumber as 0 for new customer
if(null == custNumber || StringUtils.isEmpty(custNumber)){
customer.setCustomerNumber("0");
}
if(null == customer.getStatus() || customer.getStatus().trim().equals(""))
{
customer.setStatus("Enabled");
}
getHibernateTemplate().saveOrUpdate(customer); //Throws error
//getHibernateTemplate().save(customer);
log.debug("save successful");
return findById(customer.getCustomerID());
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}



Thanks in advance for any replies,

Namrita.
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Looks like otherIDNumber is null when it should not be. Can you check if thats true?
Namrita Mohanty
Ranch Hand

Joined: Sep 26, 2010
Posts: 35
Yeah The value of the Attribute "otherIDNumber" is null but in the database I have checked the property of that attribute. It says allow Null = Yes.

In this case it should work right?

Thanks,
Namrita
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
What about your mapping file? Does it allow null for otherIDNumber?
Namrita Mohanty
Ranch Hand

Joined: Sep 26, 2010
Posts: 35
Hello SanathanaKrishanan,

Could you please specify about which mapping file should I be checking out for the database column properties. I am not aware of it.

Thanks,
Namrita
Namrita Mohanty
Ranch Hand

Joined: Sep 26, 2010
Posts: 35
Even if i make the otherIDNumber work by hardcoding the value I get the below error:

org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.ing.workstation.resource.appcapture.dto.NationalityCodes; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.ing.workstation.resource.appcapture.dto.NationalityCodes

Could you please suggest. Anyone??

Thanks,
Namrita
Namrita Mohanty
Ranch Hand

Joined: Sep 26, 2010
Posts: 35
I found out the mapping file :

<property name="otheridnumber" type="java.lang.String">
<column name="OTHERIDNUMBER" length="50" />
</property>

it doesn't say if it can be "null" or not like it says for some of the fields [though not all]

<many-to-one name="nbgender" class="com.hib.reverse.Nbgender" fetch="select">
<column name="GENDERCODE" precision="22" scale="0" not-null="true" />
</many-to-one>

Thanks,
Namrita
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
Namrita Mohanty wrote:I found out the mapping file :

<property name="otheridnumber" type="java.lang.String">
<column name="OTHERIDNUMBER" length="50" />
</property>

it doesn't say if it can be "null" or not like it says for some of the fields [though not all]

<many-to-one name="nbgender" class="com.hib.reverse.Nbgender" fetch="select">
<column name="GENDERCODE" precision="22" scale="0" not-null="true" />
</many-to-one>

Thanks,
Namrita


can you try with
<property name="otheridnumber" type="java.lang.String">
<column name="OTHERIDNUMBER" length="50" not-null="false" />
</property>

SCJP 1.5(97%) My Blog
Namrita Mohanty
Ranch Hand

Joined: Sep 26, 2010
Posts: 35
Thanks. It did solve my problem.

Regards,
Namrita
Namrita Mohanty
Ranch Hand

Joined: Sep 26, 2010
Posts: 35
So many days I was not encountering this similar problem with the fix provided. But again I did run into this with another attribute:

"ORA-01400: cannot insert NULL into ("KUNALB"."NBCUSTOMER"."SMOKERSTATUSCODE")"

In the mapping file I have set its "not-null" value as "false" still am facing this.

Suggestions please...
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
How is the column defined inside your DB? The not-null-attribute is just for creating the schema by hibernate.
Namrita Mohanty
Ranch Hand

Joined: Sep 26, 2010
Posts: 35
When I looked into the Database scripts that was run, there it was defined as:
"SMOKERSTATUSCODE" NOT NULL

I did change it to NULL and it is working now.... Earlier I had checked only with the Table details wherein it said in Column Properties that NULL is allowed but still it wasnt allowing inserting NULL as the scripts were run with "NOT NULL" [I suppose]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate: Save to Database throwing exception
 
Similar Threads
Error in reading hibernate.cfg.xml after updating hibernate3.jar
Cant store data in database table Hibernat/Spring/JSF
@Transactional Annotation
Problem with nullable mapping
Hibernate not saving