• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error on inserting a new record to table:

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rohit Kumar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic