These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Saving object with null property
PostPosted: Mon Feb 02, 2009 7:33 am 
Newbie

Joined: Fri Nov 14, 2008 7:43 am
Posts: 7
Hi there!
Currently i am building an application which have a class named "Employee" . My class looks like this :

Code:
public class Employee : GenericBaseEntity, INotifyPropertyChanged
    {
        #region Fields

        private int depNum;
        private string name;
        private long nif;
        private string email;
        private BaseCompany company;
        private FamilyType famType = new FamilyType();
        private DateTime enterDate;
        private DateTime? exitDate;
        private State eState = State.ACTIVO;
       
        #endregion

        #region Properties

        public virtual int DepNum
        {
            get { return depNum; }
            set { depNum = value; }
        }

        public virtual string Name
        {
            get { return name; }
            set { name = value; }
        }

        public virtual long Nif
        {
            get { return nif; }
            set { nif = value; }
        }

        public virtual FamilyType FamType
        {
            get { return famType; }
            set { famType = value; }
        }

        public virtual DateTime EnterDate
        {
            get { return enterDate; }
            set
            {
                enterDate = value;
                NotifyPropertyChanged("EnterDate");
            }
        }

        public virtual DateTime? ExitDate
        {
            get { return exitDate; }
            set
            {
                exitDate = value;
                NotifyPropertyChanged("ExitDate");
            }
        }

        public virtual string Email
        {
            get { return email; }
            set { email = value; }
        }

        public virtual State EState
        {
            get { return eState; }
            set { eState = value; }
        }

        public virtual BaseCompany Company
        {
            get { return company; }
            set { company = value; }
        }

        #endregion

        #region Ctors

        public Employee(){}

        public Employee(string name, long nif, DateTime ed, DateTime exd, State state)
        {
            Name = name;
            Nif = nif;
            EnterDate = ed;
            ExitDate = exd;
            EState = state;
        }

        public Employee(int depNum, string name, long nif, DateTime ed, DateTime exd, State state, FamilyType ft, string email)
            :this(name, nif, ed, exd, state)
        {
            DepNum = depNum;
            famType = ft;
            Email = email;
        }

        public Employee(int depNum, string name, long nif, FamilyType ft, DateTime ed, DateTime exd, string email, State state, BaseCompany bc)
            :this(depNum, name, nif, ed, exd, state, ft,email)
        {
            Company = bc;
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        public virtual void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
            }

        }

        #endregion
    }


Company property is a foreign key and can be null . When I do session.Save(employee), Sql server throws an exception about the constraints with the Cmpanies table .

Any suggestion ?
Thanks in advance,
Ivan Frias


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 9:08 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Sounds like your table definition is wrong. Afaik NULL is allowed for foreign keys. Is it possible that you have a NOT NULL constraint on the column ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 10:29 am 
Newbie

Joined: Fri Nov 14, 2008 7:43 am
Posts: 7
wolli wrote:
Sounds like your table definition is wrong. Afaik NULL is allowed for foreign keys. Is it possible that you have a NOT NULL constraint on the column ?


Everything is correct in employess table . The foreign key can be null.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 10:37 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
What kind of exception do you get ? If it's a SQL server exception, you it's not a hibernate problem. If it's a hibernate exception try adding not-found="ignore" to the many-to-one to company. But, I think it's only used for select and not for insert/update.

Can you post the mapping file ?

Can you insert records with a null manually ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 12:48 pm 
Newbie

Joined: Fri Nov 14, 2008 7:43 am
Posts: 7
wolli wrote:
What kind of exception do you get ? If it's a SQL server exception, you it's not a hibernate problem. If it's a hibernate exception try adding not-found="ignore" to the many-to-one to company. But, I think it's only used for select and not for insert/update.

Can you post the mapping file ?

Can you insert records with a null manually ?


The exception comes from SQL Server :
NHibernate: INSERT INTO Employees (DepNum, Name, Nif, EnterDate, ExitDate, Email, EState, Id_FamType, Id_Company) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8); select SCOPE_IDENTITY(); @p0 = '1', @p1 = 'func1', @p2 = '123123123', @p3 = '10-02-2009 00:00:00', @p4 = '', @p5 = '', @p6 = '1', @p7 = '11', @p8 = ''
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in NHibernate.dll
A first chance exception of type 'NHibernate.Exceptions.GenericADOException' occurred in NHibernate.dll

Employee.hbm.xml file :
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
 
  <class name="Save.Domain.Employee, Save" table="Employees" lazy="false">
    <id name="Id" unsaved-value="-1">
      <generator class="native"/>
    </id>

    <property name="DepNum" not-null="true" />

    <property name="Name" not-null="true" />

    <property name="Nif" not-null="true" unique="true" type="long"/>

    <property name="EnterDate" not-null="true" />

    <property name="ExitDate" not-null="false"/>

    <property name="Email" not-null="false" />

    <property name="EState" not-null="true" type="Save.Domain.State, Save" lazy="false"/>
   
    <many-to-one name="FamType" not-null="true" class="Save.Domain.FamilyType, Save" column="Id_FamType" lazy="false"/>

    <many-to-one name="Company" class="Save.Domain.BaseCompany, Save" lazy="false" not-null="false" column="Id_Company" />
   
  </class>
</hibernate-mapping>


BaseCompany.hbm.xml :
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
 
  <class name="Save.Domain.BaseCompany, Save" table="Companies" lazy="false" >
    <id name="Id" column="Id_Company" unsaved-value="-1" >
      <generator class="native" />
    </id>

    <discriminator column="COMPANY_TYPE" type="string" />
   
    <property name="Name" not-null="true" unique="true" column="CmpName"/>

    <property name="ShortName" not-null="true" column="ShName"/>

    <property name="NIPC" not-null="true" unique="true" column="NIPC"/>

    <property name="State" not-null="true" type="Save.Domain.State, Save" />
   
    <many-to-one name="Business" class="Save.Domain.BusinessField, Save" not-null="true" column="Id_BusinessField" lazy="false"/>

    <bag name="Employees">
      <key column="Id" not-null="false" />
      <one-to-many class="Save.Domain.Employee, Save" />
    </bag>
   
    <subclass name="Save.Domain.GroupCompany, Save" discriminator-value="GROUP">
     
      <bag name="ChildCompanies" cascade="none" inverse="true" lazy="false">
        <key column="Id_Parent_Company"/>
        <one-to-many class="Save.Domain.Company, Save"/>
      </bag>
     
    </subclass>
   
    <subclass name="Save.Domain.Company, Save" discriminator-value="COMPANY">
     
      <many-to-one name="ParentCompany" class="Save.Domain.GroupCompany, Save" column="Id_Parent_Company" lazy="false"/>
     
      <bag name="ChildCompanies">
        <key column="Id_Parent_Company"/>
        <one-to-many class="Save.Domain.RCCompany, Save"/>
      </bag>
     
    </subclass>

    <subclass name="Save.Domain.RCCompany, Save" discriminator-value="RC">

      <many-to-one name="ParentCompany" class="Save.Domain.Company, Save" column="Id_Parent_Company" lazy="false"/>

    </subclass>

  </class>
</hibernate-mapping>


Yes, I executed the following comand and it worked :

Code:
INSERT INTO Companies(COMPANY_TYPE, CmpName, ShName, NIPC,State, Id_BusinessField, Id_Parent_Company) VALUES('Company','Empresa 01', 'EM 01', 123123123, 1, 3, NULL);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 1:22 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I can't see any obvious problem but I wonder if your unsaved-value causes this. Are you sure that your initial value is set to -1 for new objects. If not hibernate probably gets confused and tries to update instead of insert.

Do you have a special reason why you use -1 ? Recommended value would be 0 with generator native.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.