Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Object Relational Mapping and the fly likes Error on inserting a new record to table: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Error on inserting a new record to table:" Watch "Error on inserting a new record to table:" New topic

Error on inserting a new record to table:

Rohit Kumar
Ranch Hand

Joined: Jul 19, 2007
Posts: 35
I am getting the following error when I try to insert a new record using Hibernat. Below is my code. Could anyone suggest me where am I going wrong.

Table: Capital_improve_proj
Pk: cip_id
Fk: facility_id

Table: Facility
Pk: facility_id

The relation between Facility and Capital_improve_proj is one-to-many.(one facility_id can have many cip_id)

This is my little part of Capital_improve_proj.hbm.xml file:
<class name=" employ.dto.CapitalImproveProj" table="CAPITAL_IMPROVE_PROJ" schema="dbo" catalog="Aviation">
<id name="cipId" type="integer">
<column name="CIP_ID" />
<generator class="increment" />
<many-to-one name="facility" class="employ.dto.Facility" fetch="select" update="false" cascade="none">
<column name="FACILITY_ID" not-null="true" />

This is Facility.hbm.xml
<class name="employ.dto.Facility" table="FACILITY" schema="dbo" catalog="Aviation">
<id name="facilityId" type="integer">
<column name="FACILITY_ID" />
<generator class="native" />

I have an add button on one of the screen. On clicking this it should enter a new record to databse (Capital_improve_proj table).

This is my code to insert a new record to table:
cip = (CapitalImproveProj)map.get("cip");
Session session = HibernateUtil.getInstance().getSession();
session.beginTransaction();; /////ERROR OCCURING AT THIS LINE.

The error message is :
org.hibernate.PropertyValueException: not-null property references a null or transient value: gov.gdot.ooit.asm.dto.CapitalImproveProj.facility
at org.hibernate.engine.Nullability.checkNullability(
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(
at org.hibernate.event.def.AbstractSaveEventListener.performSave(
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(
at org.hibernate.impl.SessionImpl.fireSave(
at gov.gdot.ooit.asm.trans.CipTrans.copyCip(
at gov.gdot.ooit.asm.model.EditCip.add(
at gov.gdot.ooit.asm.command.AddCommand.execute(
at gov.gdot.ooit.asm.control.ASMController.control(

Could anyone help on this.

Shailesh Kini
Ranch Hand

Joined: Oct 17, 2001
Posts: 153
Hi Rahul,

From the error log you posted it seems like the CapitalImproveProj object returned from map probably has a null referece to the Facility reference. Please check that and set it before saving. That should fix the problem.

Shailesh Kini.
Rohit Kumar
Ranch Hand

Joined: Jul 19, 2007
Posts: 35

Thnx for the reply. yes..facility Reference is null.

but am also using session.saveOrUpdate(cip) with facility reference is null as well and is working fine when i edit and update the screen

Is there any difference. I will try to set facility reference before saving and let you know the result.

Shailesh Kini
Ranch Hand

Joined: Oct 17, 2001
Posts: 153
Hi Rahul,

Yes there is a difference. When you are inserting a new record in CAPITAL_IMPROVE_PROJ you have a foreign key reference to Facility which cannot be null. The insert fails for this reason.

Alternately, when you fire an update query, you probably do a find first. The find loads the Facility reference and that's the reason update works. There is an association between your Capital_improve_proj and Facility record.
Rohit Kumar
Ranch Hand

Joined: Jul 19, 2007
Posts: 35
Hi Shailesh,

Thanks for the reply.I set the facilityId and is working fine, able to insert new records to table.

but, when I using the same funcationality for the second time, facilityId is getting null which throws me exceptions. Any suggestions
Shailesh Kini
Ranch Hand

Joined: Oct 17, 2001
Posts: 153

I am quite not sure when you say "second time". Does it fail when you create another new record into Capital_improve_proj table? From the code that you provided above it's not clear where and how facilityId is set. I should be able to help you if you provide the actual code that sets the facility and how it's called.

Debugging should help identifying this problem. Debugger is your best friend.
[ August 14, 2007: Message edited by: Shailesh Kini ]
I agree. Here's the link:
subject: Error on inserting a new record to table:
jQuery in Action, 3rd edition