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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Not able to insert into DB getting SqlException
PostPosted: Wed Aug 12, 2009 8:31 am 
Newbie

Joined: Wed Aug 12, 2009 8:23 am
Posts: 11
Hi
am trying to insert an object into mapped table in the database. But i get an error while doing so. Please see the error details below.
<desc>
Cannot insert the value NULL into column 'ID', table 'NHibernateSimpleDemo.dbo.ConfigData'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."
</desc>

Looks like a minor problem. :( but not able to figure out myself where am going wrong. A help will be greatly appreciated.

Thanks,
Siyad


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Wed Aug 12, 2009 11:09 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
What kind of id generator do you use ? Please post the mapping file.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Wed Aug 12, 2009 2:07 pm 
Newbie

Joined: Wed Aug 12, 2009 8:23 am
Posts: 11
Hi,

Thanks for the reply.... well am using 'identity' as the generator. Also i have tried 'native' but still getting the same error.
Am able to make a select query operation. This problem only happens in case of INSERT operation.


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Wed Aug 12, 2009 8:04 pm 
Newbie

Joined: Wed Aug 12, 2009 8:02 pm
Posts: 1
This is an interesting post.. thank you for sharing
plan solution commission de surendettement - commission de surendettement, vous pouvez demander un dossier de surendettement.plan solution commission de surendettement


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Thu Aug 13, 2009 1:39 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
It's hard to say anything without the mapping file and the code where you save the object.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Thu Aug 13, 2009 9:01 am 
Newbie

Joined: Wed Aug 12, 2009 8:23 am
Posts: 11
Hi
Please find the mapping file contents below.

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   default-lazy="false"
                   assembly="NHibernateTest1"
                   namespace="NHibernateTest1">
  <class name="UIConfiguration" table="ConfigData" >
    <id name="ID" type="System.Int32" >
      <column name="ID" update="false" insert="false"/>
      <generator class="identity"/>
    </id>
    <property name="SelectedUIItems" column="SelectedUIItems" type="System.String" not-null="true" length="250"/>
  </class>
</hibernate-mapping>


Here is the object representation that maps to the DB entity in above mapping
Code:
public class UIConfiguration
    {
        private int p_ID = -1;
         private string _listOfUIItems;

        public UIConfiguration()
        {

        }

        public UIConfiguration(Int32 id)
        {
            ID = id;
        }

        /// <summary>
        /// The ID of the product.
        /// </summary>
        public virtual int ID
        {
            get { return p_ID; }
            set { p_ID = value; }
        }
        public virtual string SelectedUIItems
        {
            get
            {
                return _listOfUIItems;
            }

            set
            {
                _listOfUIItems = value;
            }
        }

        public override int GetHashCode()
        {
            return (GetType().FullName + "|" +
                    ID.ToString()).GetHashCode();
        }
    }

Here is the class that manages NHibernate operations

Code:
public enum SessionAction { Begin, Continue, End, BeginAndEnd }
    public class PersistenceManager
    {
        #region Declarations

        // Member variables
        private ISessionFactory m_SessionFactory = null;
        private ISession m_Session = null;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public PersistenceManager()
        {
           
            this.ConfigureNHibernate();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            m_SessionFactory.Dispose();
        }

        #endregion
/// <summary>
        /// Close this Persistence Manager and release all resources (connection pools, etc). It is the responsibility of the application to ensure that there are no open Sessions before calling Close().
        /// </summary>
        public void Close()
        {
            m_SessionFactory.Close();
        }

/// <summary>
        /// Saves an object and its persistent children.
        /// </summary>
        public void Save<T>(T item)
        {
            using (ISession session = m_SessionFactory.OpenSession())
            {
                using (session.BeginTransaction())
                {
                    session.SaveOrUpdate(item);
                    session.Transaction.Commit();
                }
            }
        }

/// <summary>
        /// Configures NHibernate and creates a member-level session factory.
        /// </summary>
        private void ConfigureNHibernate()
        {
            // Initialize
            Configuration cfg = new Configuration();
            cfg.Configure();

            /* Note: The AddAssembly() method requires that mappings be
             * contained in hbm.xml files whose BuildAction properties
             * are set to ‘Embedded Resource’. */

            // Add class mappings to configuration object
            Assembly thisAssembly = typeof(TaskPadConfiguration).Assembly;
            cfg.AddAssembly(thisAssembly);

            // Create session factory from configuration object
            m_SessionFactory = cfg.BuildSessionFactory();
        }

        #endregion
    }

Here is the client code..........

Code:
static void Main(string[] args)
        {
            PersistenceManager persistManager = new PersistenceManager();
            UIConfiguration item = new UIConfiguration();
            item.SelectedUIItems = "Item1";
            persistManager.Save<UIConfiguration>(item);
        }


Please let me know if you need any other information.


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Thu Aug 13, 2009 9:05 am 
Newbie

Joined: Wed Aug 12, 2009 8:23 am
Posts: 11
Pls ignore the update & insert attributes in the mapping, they are not valid. I was trying a different option with that. I forgot to remove them before posting.

<column name="ID" update="false" insert="false"/>


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Thu Aug 13, 2009 9:19 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
The default unsaved value for identity is 0. Since you initialize it with -1, hibernate thinks it's an already persisted object and tries to update ! Is there a special reason for using "-1". It's highly recommended to use the default. If you have to use -1, you have to specifiy the unsaved-value in the id mapping:

<id name="ID" type="System.Int32" unsaved-value="-1">

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Mon Aug 17, 2009 5:02 am 
Newbie

Joined: Wed Aug 12, 2009 8:23 am
Posts: 11
Hi Wolli,

Sorry for the delay in reply. I was out of station for sometime.
Well, it tried what you suggested. But it seems that particular attribute is not recognized it seems. Am getting not defined/undefined error related to that particular attribute.
Is there anything else i have to set up to resolve those attributes?


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Mon Aug 17, 2009 5:17 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
This should definitely work:

<id name="ID" column="id" type="System.Int32" unsaved-value="-1">
<generator class="identity"/>
</id>

if you specify the column as a separate tag, the attribute may have to be put there:

<id name="ID" type="System.Int32" >
<column name="ID" unsaved-value="-1">
<generator class="identity"/>
</id>

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Mon Aug 17, 2009 7:05 am 
Newbie

Joined: Wed Aug 12, 2009 8:23 am
Posts: 11
I have tried the first option. Now i am not getting any compile error. But still am not able to get rid of the actual exception.
:(
Anyother alternatives?


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Mon Aug 17, 2009 8:03 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try initializing the value with 0 just to make sure, insert+update are working at all.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Mon Aug 17, 2009 8:06 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
And make sure that the id column in your table is an identity column.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Tue Aug 18, 2009 8:01 am 
Newbie

Joined: Wed Aug 12, 2009 8:23 am
Posts: 11
Hi,

I have tried the first option. :( still no success.
Please see the Table structure below.

Column Name DataType AllowNull
------------- --------- ---------
ID int
SelectedUIItem nvarchar(MAX)


Top
 Profile  
 
 Post subject: Re: Not able to insert into DB getting SqlException
PostPosted: Tue Aug 18, 2009 12:31 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Using generator "identity" or "native" means the database has to generate the id. YOu have to define your id column like that:

[id] [int] IDENTITY(1,1) NOT NULL,

or

[id] [int] IDENTITY(0,1) NOT NULL,

depending on if you want to start with 1 or 0. I recommend the first one and use "0" as unsaved value.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.