-->
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.  [ 5 posts ] 
Author Message
 Post subject: NHibernate index problem
PostPosted: Wed Nov 22, 2006 6:08 am 
Beginner
Beginner

Joined: Wed Nov 22, 2006 5:59 am
Posts: 29
Location: London
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

1.2.0 Beta 1


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="BCS.Communications.Data.Tables" assembly="BCS.Communications.Data">
<class name="Customer">
<id name ="CustomerId" column="CustomerId" type="int">
<generator class="native" />
</id>
<property name="CustomerName">
<column name="CustomerName" sql-type="NVARCHAR(MAX)"/>
</property>
<list name="Orders" cascade="all" fetch="join">
<key column="OrderId" foreign-key="fk_customer_order"/>
<index column="idx" type="byte"/>
<one-to-many class="CustomerOrder" />
</list>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="BCS.Communications.Data.Tables" assembly="BCS.Communications.Data">
<class name="CustomerOrder">
<id name ="OrderId" column="OrderId" type="int">
<generator class="native" />
</id>
<property name="OrderName">
<column name="OrderName" sql-type="NVARCHAR(MAX)"/>
</property>
<many-to-one name="CustomerId" class="Customer" column="CustomerId" foreign-key="fk_customer_order" />
</class>
</hibernate-mapping>


I'm having problems with the index created for using lists in the mapping files. I am using an IList in my entity class for customer orders. The problem is when you add a new customer order the idx index field in the database is set to null. The customerId field in CustomerOrder is set correctly but I can't seem to get the index field to initialize. The code I'm using to test this is below.


Code:
        public void TestRelationship()
        {
            try
            {
                ISession session = NHibernateHelper.GetCurrentSession();
                Customer c = new Customer();
                c.CustomerName = "Test";
                c.AddOrder(new CustomerOrder());
                session.SaveOrUpdateCopy(c);
            }
            catch (Exception e)
            {
            }
        }
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 9:24 am 
Beginner
Beginner

Joined: Thu Jul 06, 2006 4:44 am
Posts: 31
You are adding the "new CutomerOrder" to your own list. The hibernate has no idea how to persist that object. I think you must add it to your Orders list to persist it. What do you do in your AddOrder method?

greetings
markov


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 9:36 am 
Beginner
Beginner

Joined: Wed Nov 22, 2006 5:59 am
Posts: 29
Location: London
Thanks for your reply. My AddOrder method looks like this is is contained in my Customer class

Code:
        public virtual void AddOrder(CustomerOrder order)
        {
            order.CustomerId = this;
            Orders.Add(order);
        }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 9:59 am 
Beginner
Beginner

Joined: Thu Jul 06, 2006 4:44 am
Posts: 31
Yoo can try this in your customer mapping:

<bag name="Orders" inverse="true" cascade="all" fetch="join">
<key column="OrderId" foreign-key="fk_customer_order"/>
<one-to-many class="BCS.Communications.Data.Tables.CustomerOrder,BCS.Communications.Data" />
</bag>

At least I have defined it in my code like this. (maybe it is different for 1.2.0 version)

cheers

markov


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 10:14 am 
Beginner
Beginner

Joined: Wed Nov 22, 2006 5:59 am
Posts: 29
Location: London
Thanks that seems to of fixed the problem.

Rob


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