File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Spring in Action this week in the Spring forum!
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
Author

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:
<hibernate-mapping>
<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" />
</id>
<many-to-one name="facility" class="employ.dto.Facility" fetch="select" update="false" cascade="none">
<column name="FACILITY_ID" not-null="true" />
</many-to-one>

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

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();
session.save(cip); /////ERROR OCCURING AT THIS LINE.
session.getTransaction().commit();
session.flush();
System.err.println("###");

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(Nullability.java:72)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:284)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at gov.gdot.ooit.asm.trans.CipTrans.copyCip(CipTrans.java:304)
at gov.gdot.ooit.asm.model.EditCip.add(EditCip.java:55)
at gov.gdot.ooit.asm.command.AddCommand.execute(AddCommand.java:44)
at gov.gdot.ooit.asm.control.ASMController.control(ASMController.java:93)


Could anyone help on this.

Thanks..
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
Hi..

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.

Thanks,,
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
Rahul,

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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Error on inserting a new record to table: