Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: Save to Database throwing exception

 
Namrita Mohanty
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like otherIDNumber is null when it should not be. Can you check if thats true?
 
Namrita Mohanty
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about your mapping file? Does it allow null for otherIDNumber?
 
Namrita Mohanty
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Namrita Mohanty
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. It did solve my problem.

Regards,
Namrita
 
Namrita Mohanty
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is the column defined inside your DB? The not-null-attribute is just for creating the schema by hibernate.
 
Namrita Mohanty
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic