Hibernate Books

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Object Cannot be save with child objects because of FK Key
PostPosted: Tue May 29, 2012 4:51 am 
Newbie

Joined: Tue May 29, 2012 4:20 am
Posts: 4
Hello, I am using nhibernate with an Oracle database and have got the following Problem:
I have got one Object View and a child object ViewItem.
The problem is, I want to save the view and want the child Items be saved with the view using cascade="all".
It looks like this:
Code:
View view = new View() { C_TIME = DateTime.Now, C_USER = "e", NAME = test, U_TIME = DateTime.Now, U_USER = "f" };
ViewItem vli = new ViewItem() { C_TIME = DateTime.Now, C_USER = "e", NAME = "Test2", U_TIME = DateTime.Now, U_USER = "f", HEADER = "we", HIDDEN = 1, MANDATORY = 4 };
view.ViewListItems.Add(vli);
session.Save(view);

I get an Exception: {"ORA-02291: Integritäts-Constraint (CALNET_NEW_ABN.VIEWITEM_VIEW_FK) verletzt - übergeordneter Schlüssel nicht gefunden\n"}.
How does NHibernate work also saving the child objects?
These are the classes and mapping files:

Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="CALConsult.Portal.Domain.Model" namespace="CALConsult.Portal.Domain.Model" xmlns="urn:nhibernate-mapping-2.2">
  <class name="View" table="`View`" lazy="true" >
    <id name="Id" type="Decimal">
      <column name="ID" sql-type="NUMBER" not-null="true" />
      <generator class="assigned" />
    </id>
    <property name="NAME">
      <column name="NAME" sql-type="VARCHAR2" not-null="true" />
    </property>
    <property name="DELFLAG">
      <column name="DELFLAG" sql-type="NUMBER" not-null="true" />
    </property>
    <property name="C_TIME">
      <column name="C_TIME" sql-type="DATE" not-null="true" />
    </property>
    <property name="C_USER">
      <column name="C_USER" sql-type="VARCHAR2" not-null="true" />
    </property>
    <property name="U_TIME">
      <column name="U_TIME" sql-type="DATE" not-null="false" />
    </property>
    <property name="U_USER">
      <column name="U_USER" sql-type="VARCHAR2" not-null="false" />
    </property>
    <bag name="ViewListItems" cascade="all">
      <key column="VIEWID" />
      <one-to-many class="ViewItem" />
    </bag>
  </class>
</hibernate-mapping>
using System;
using System.Collections.Generic;

namespace CALConsult.Portal.Domain.Model
{
    public class View : Entity<decimal>
    {
        public View()
        {
            ViewListItems = new List<ViewItem>();
        }
      
        public virtual IList<ViewItem> ViewListItems { get; set; }

        public virtual string NAME { get; set; }

        public virtual long DELFLAG { get; set; }

        public virtual DateTime C_TIME { get; set; }

        public virtual string C_USER { get; set; }

        public virtual DateTime? U_TIME { get; set; }

        public virtual string U_USER { get; set; }
    }
}

Teh view Item has The mapping file:
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="CALConsult.Portal.Domain.Model" namespace="CALConsult.Portal.Domain.Model" xmlns="urn:nhibernate-mapping-2.2">
  <class name="ViewItem" table="VIEWITEM" lazy="true" >
    <id name="Id" type="Decimal">
      <column name="ID" sql-type="NUMBER" not-null="true" />
      <generator class="assigned" />
    </id>
    <property name="VIEWID" type="Decimal">
      <column name="VIEWID" sql-type="NUMBER" not-null="true" />
    </property>
    <property name="NAME">
      <column name="NAME" sql-type="VARCHAR2" not-null="true" />
    </property>
    <property name="HEADER">
      <column name="HEADER" sql-type="VARCHAR2" not-null="true" />
    </property>
    <property name="MANDATORY">
      <column name="MANDATORY" sql-type="NUMBER" not-null="true" />
    </property>
    <property name="DELFLAG">
      <column name="DELFLAG" sql-type="NUMBER" not-null="true" />
    </property>
    <property name="C_TIME">
      <column name="C_TIME" sql-type="DATE" not-null="true" />
    </property>
    <property name="C_USER">
      <column name="C_USER" sql-type="VARCHAR2" not-null="true" />
    </property>
    <property name="U_TIME">
      <column name="U_TIME" sql-type="DATE" not-null="false" />
    </property>
    <property name="U_USER">
      <column name="U_USER" sql-type="VARCHAR2" not-null="false" />
    </property>
      <property name="TYPE">
        <column name="TYPE" sql-type="VARCHAR2" not-null="false" />
      </property>
      <property name="HIDDEN">
        <column name="HIDDEN" sql-type="NUMBER" not-null="true" />
      </property>
  </class>
</hibernate-mapping>
using System;
using System.Collections.Generic;

namespace CALConsult.Portal.Domain.Model
{
    public class ViewItem : Entity<decimal>
    {
        public ViewItem()
        {
        }

        public virtual decimal VIEWID { get; set; }

        public virtual decimal CONFIGID { get; set; }

        public virtual string NAME { get; set; }

        public virtual string HEADER { get; set; }

        public virtual long MANDATORY { get; set; }

        public virtual long DELFLAG { get; set; }

        public virtual DateTime C_TIME { get; set; }

        public virtual string C_USER { get; set; }

        public virtual DateTime? U_TIME { get; set; }

        public virtual string U_USER { get; set; }

        public virtual long? ORDERNO { get; set; }

        public virtual string TYPE { get; set; }

        public virtual long HIDDEN { get; set; }
    }
}


Top
 Profile  
 
 Post subject: Re: Object Cannot be save with child objects because of FK Key
PostPosted: Tue May 29, 2012 6:29 am 
Newbie

Joined: Tue May 29, 2012 4:20 am
Posts: 4
One additional Information: The german errormessage means, that the Foreign Key is violated.


Top
 Profile  
 
 Post subject: Re: Object Cannot be save with child objects because of FK Key
PostPosted: Tue Jun 05, 2012 5:15 am 
Newbie

Joined: Tue May 29, 2012 4:20 am
Posts: 4
I have solved it, the solution is in http://dustyreagan.com/how-to-nhibernate-parentchild/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.