-->
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.  [ 2 posts ] 
Author Message
 Post subject: Native SQL Query
PostPosted: Mon Dec 10, 2007 8:30 am 
Regular
Regular

Joined: Mon Oct 02, 2006 12:03 pm
Posts: 62
I need to obtain a list of Stock Objects, and I use a native SQL in order to obtain this list -->

Code:
NHibernate.ISession session = this.session_factory.OpenSession();
            NHibernate.ISQLQuery query = session.CreateSQLQuery("SELECT A.* {stock.Id.Article.*}, P.* {stock.Id.Place.*}, CAST(AMOUNT AS SIGNED INT) {stock.Amount} " +
                            "FROM STOCK_MOVEMENTS M, ARTICLE A {stock.Id.Article}, PLACE P {stock.Id.Place} WHERE M.TO_PLACE_ID IS NOT NULL").AddEntity("stock", typeof(Model.Consulting.Stock));
            return query.List<Model.Consulting.Stock>();


The exception message is -->

Quote:
No column name found for property [Id.Article.*] for alias [stock] [SELECT A.* {stock.Id.Article.*}, P.* {stock.Id.Place.*}, CAST(AMOUNT AS SIGNED INT) {stock.Amount} FROM STOCK_MOVEMENTS M, ARTICLE A {stock.Id.Article}, PLACE P {stock.Id.Place} WHERE M.TO_PLACE_ID IS NOT NULL]


Code:
    public class Stock
    {
        private Model.Consulting.StockId id;
        private int quantity;

        protected Stock () {
            this.id = new StockId();
            this.quantity = -1;
        }

        public Stock(Model.Entities.ComplexArticle article, Model.Entities.Place site, int quantity)
        {
            this.id = new StockId(article, site);
            this.quantity = quantity;
        }

        #region Properties

        public virtual Model.Consulting.StockId Id { get { return id; } set { id = value; } }

        public virtual Model.Entities.ComplexArticle Article { get { return this.id.Article; } set { this.id.Article = value; } }

        public virtual Model.Entities.Place Site { get { return this.id.Site; } set { this.id.Site = value; } }

        public virtual int Quantity { get { return quantity; } set { quantity = value; } }

        public virtual double Value { get { return this.id.Article.Price * this.quantity; } }

        #endregion

    }

    public class StockId
    {
        private Model.Entities.ComplexArticle article;
        private Model.Entities.Place site;

        public StockId()
        {
            this.article = null;
            this.site = null;
        }

        public StockId(Model.Entities.ComplexArticle article, Model.Entities.Place site)
        {
            this.article = article;
            this.site = site;
        }

        #region Properties

        public virtual Model.Entities.ComplexArticle Article { get { return article; } set { article = value; } }

        public virtual Model.Entities.Place Site { get { return site; } set { site = value; } }

        #endregion

        #region ToString, Equals, HashCode

        public override bool Equals(object obj)
        {
            if (this == obj) return true;
            if (obj == null || !obj.GetType().Equals(this.GetType())) return false;

            Model.Consulting.StockId altre = (Model.Consulting.StockId)obj;
            return this.Article == altre.Article && this.Site == altre.Site;
        }

        public override int GetHashCode()
        {
            return this.Site.GetHashCode() + this.Article.GetHashCode() + 74;
        }

        #endregion
    }


I don't how to create this sentence.
Can you help me, please.
I will appreciate a lot your help.
Thanks for all.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 2:38 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
There seems to be several problems with your query. First, you can't alias '*', so you select should probably start
Code:
SELECT A.* {stock.Id.Article.*}, P.* {stock.Id.Place.*}, ...
.

Second, did you forget to query for quantity?

Third, you didn't limit your join in the WHERE, so you'll get a cross-join.

Fourth, you need to reference relationships with AddJoin() (see http://www.hibernate.org/hib_docs/nhibe ... ml#d0e9020 ).

HTH!


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