-->
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: Persisting with Order-By
PostPosted: Wed Jul 25, 2007 2:30 pm 
Newbie

Joined: Wed Jul 25, 2007 2:16 pm
Posts: 8
I have a class that contains a set of ordered products. The set is ordered by a column 'Order' in my table and the order column does not allow nulls. When I persist this class, I get an error: cannot insert NULL into column 'Order.'

So the problem is that the Order column is not mapping when i persist but the set of products is being ordered correctly. what is the proper way of doing this?


Code:
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
                   namespace="Com.WildTangent.ProductCatalog.Data"
                   assembly="ProductCatalog">
    <class name="RuleGroupSlot" table="t_RuleGroupSlot">
        <cache usage="read-write" />

        <id name="Id" column="RuleGroupSlotId" type="int">
            <generator class="native" />
        </id>

        <set name="OrderedProducts" table="t_ProductSlotOrder" order-by="[Order]">
            <key column="RuleGroupSlotId" />
            <many-to-many column="ProductId" class="Product" />
        </set>

    </class>
</hibernate-mapping>



Code:
public class RuleGroupSlot
    {
        public virtual int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        // We don't have backward references to RuleGroup and Slot here for efficiency reasons

        public IProductList GetOrderedProducts()
        {
            ProductList productList = new ProductList();
            productList.Products = CollectionsUtil.ToGenericList<Product>(OrderedProducts);
            return productList;
        }

        public virtual ISet OrderedProducts
        {
            get { return _orderedProducts; }
            set { _orderedProducts = value; }
        }

        private int _id;
        private ISet _orderedProducts;
    }
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 2:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Instead of using a <set>, you may use a <list> instead:

Code:
        ...
        <list name="OrderedProducts" table="t_ProductSlotOrder">
            <key column="RuleGroupSlotId" />
            <index column="`Order`"/>
            <many-to-many column="ProductId" class="Product" />
        </list>
        ...


That way, you don't need to manage the Order column at all.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 4:02 pm 
Newbie

Joined: Wed Jul 25, 2007 2:16 pm
Posts: 8
i get an exception when doing this. i changed to a list..
[NHibernate.LazyInitializationException] = {"Failed to lazily initialize a collection"}

here's my code:

Code:
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
                   namespace="Com.WildTangent.ProductCatalog.Data"
                   assembly="ProductCatalog">
    <class name="RuleGroupSlot" table="t_RuleGroupSlot">
        <cache usage="read-write" />

        <id name="Id" column="RuleGroupSlotId" type="int">
            <generator class="native" />
        </id>

        <list name="OrderedProducts" table="t_ProductSlotOrder">
            <key column="RuleGroupSlotId" />
         <index column="[Order]" />
            <many-to-many column="ProductId" class="Product" />
        </list>

    </class>
</hibernate-mapping>


Code:
public class RuleGroupSlot
    {
        public virtual int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        // We don't have backward references to RuleGroup and Slot here for efficiency reasons

        public IProductList GetOrderedProducts()
        {
            ProductList productList = new ProductList();
            productList.Products = CollectionsUtil.ToGenericList<Product>(OrderedProducts);
            return productList;
        }

        public virtual IList OrderedProducts
        {
            get { return _orderedProducts; }
            set { _orderedProducts = value; }
        }

        private int _id;
        private IList _orderedProducts;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 4:27 pm 
Newbie

Joined: Wed Jul 25, 2007 2:16 pm
Posts: 8
I fixed my issue here. I didn't realize that you must include back ticks around the index column instead of brackets. brackets causes nhibernate to fail miserably.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 4:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Glad it worked for you.

_________________
Karl Chu


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.