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.  [ 3 posts ] 
Author Message
 Post subject: Insert parent + childs
PostPosted: Tue Feb 12, 2008 2:44 pm 
Beginner
Beginner

Joined: Mon Jan 14, 2008 10:58 am
Posts: 24
I have read thousands of threads but I still don't understand! :(
I'll just getting: Cannot insert the value NULL into column 'SupplierId' in table Brands.
What am I doing wrong?

Code:
        string AssamblyName = System.Configuration.ConfigurationManager.AppSettings["DefaultEntities"].ToString();
        Configuration cfg = new Configuration();
        cfg.AddAssembly(AssamblyName);
       
       
        ISessionFactory factory = cfg.BuildSessionFactory();
        ISession session = factory.OpenSession();
        ITransaction transaction = session.BeginTransaction();

        Supplier s = new Supplier();
        s.Name = "foo";
        s.CreateDate = DateTime.Now;
        s.LastUpdatedDate = s.CreateDate;

        s.Brands = new List<Brand>();
        Brand b = new Brand("bar");
        s.Brands.Add(b);
        session.Save(s);

        transaction.Commit();

        session.Close();
        session.Dispose();

        Response.Write("oki");


Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Swh.Entities.Supplier, Swh.Entities" table="Suppliers">
      <id name="Id" column="SupplierId" type="int" unsaved-value="0">
         <generator class="identity" />
      </id>
      <property name="Name" column="SupplierName" type="String"/>
      <property name="CreateDate" column="CreateDate" type="DateTime"/>
      <property name="LastUpdatedDate" column="LastUpdatedDate" type="DateTime"/>
      <bag name="Brands" lazy="true" cascade="all-delete-orphan" inverse="true">
         <key>
            <column name="SupplierId" not-null="true"/>
         </key>
         <one-to-many class="Swh.Entities.Brand, Swh.Entities"/>
      </bag>
   </class>
</hibernate-mapping>


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Swh.Entities.Brand, Swh.Entities" table="Brands">
      <id name="Id" column="BrandId" type="int" unsaved-value="0">
         <generator class="identity" />
      </id>
      <many-to-one name="Supplier" class="Swh.Entities.Supplier, Swh.Entities" column="SupplierId" />
      <property name="Name" column="BrandName" type="String"/>
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 3:15 pm 
Beginner
Beginner

Joined: Mon Jan 14, 2008 10:58 am
Posts: 24
b.Supplier = s;


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 6:21 pm 
Regular
Regular

Joined: Tue Dec 25, 2007 3:41 pm
Posts: 57
Location: Argentina
Remove the inverse=true since the Supplier isn't the reverse side of the relationship and try again.

_________________
Dario Quintana


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.