-->
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.  [ 4 posts ] 
Author Message
 Post subject: retrieve object by 2 ids
PostPosted: Tue Jul 14, 2009 4:14 pm 
Newbie

Joined: Sun Jul 01, 2007 4:10 am
Posts: 5
Hi,

I don´t know the solution for this. I will try to explain..

I have a product class and a user class and a price class.

The price class is mapped many-to-one for both user and product class.

The price class, holds the ProductId the UserId and the price(varchar)

the problem is when i click on a product to show information, i want to bind this price to a textbox, (because the user can tell the price that they gave for the same product)

So when they click the "show product" button a textbox with the users price should appear and it most match the productid and the userid.

can anyone tell me the code for this? I can send the code that i have tried todo..

Code:
private Price GetPrice(int productId, int userId)
    {
        try
        {
            string hql = "SELECT count(price.Id) from Price price WHERE price.Product.Id = :productId AND price.User.Id = :userId";

            IList<Query> queries = new List<Query>();
            Query q = new Query();
            q.ParamInt = productId;
            q.ParamName = "productId";
            queries.Add(q);

            Query q2 = new Query();
            q2.ParamInt = userId;
            q2.ParamName = "userId";
            queries.Add(q2);

            int count = DataManagement.CoreRepository.RunCountStatement(hql, queries);

            if (count > 0)
            {
                IList<Query> fetchQueries = new List<Query>();
                Query fetchQ = new Query();
                fetchQ.ParamName = "Product.Id";
                fetchQ.ParamInt = productId;
                fetchQ.ParamEvaluationType = EvaluationType.Equals;
                fetchQueries.Add(fetchQ);

                Query fetchQ2 = new Query();
                fetchQ2.ParamName = "User.Id";
                fetchQ2.ParamInt = userId;
                fetchQ2.ParamEvaluationType = EvaluationType.Equals;
                fetchQueries.Add(fetchQ2);

                Price p = DataManagement.CoreRepository.RunQuery<Price>(fetchQueries);

                return p.BuyPrice;
            }
}

        }



can someone edit the code to get it to work?

i get the product as querId and the user from the session
GetPrice(_productId,_userId);


Top
 Profile  
 
 Post subject: Re: retrieve object by 2 ids
PostPosted: Thu Jul 16, 2009 2:20 pm 
Newbie

Joined: Sun Jul 01, 2007 4:10 am
Posts: 5
no one?


Top
 Profile  
 
 Post subject: Re: retrieve object by 2 ids
PostPosted: Fri Jul 17, 2009 9:29 am 
Newbie

Joined: Sun Jul 01, 2007 4:10 am
Posts: 5
changed to this code:
Code:
    private Price GetPrice(int productId, int userId)
    {       
       
            string hql = "FROM Price price WHERE price.Product.Id = :productId AND price.User.Id = :userId";

            IList<Query> queries = new List<Query>();
            Query q = new Query();
            q.ParamInt = productId;
            q.ParamName = "Product";
            queries.Add(q);

            Query q2 = new Query();
            q2.ParamInt = userId;
            q2.ParamName = "User";
            queries.Add(q2);

            Price price = (Price)DataManagement.CoreRepository.RunQuery<Price>(hql,queries);

            return price;
       
    }


and now i´m getting this error:
Quote:
NHibernate.QueryException: Named parameter does not appear in Query: Product[FROM DAL.Domain.Product.Price price WHERE price.Product.Id = :productId AND price.User.Id = :userId]


whats the error? can´t figure it out. anyone?


Top
 Profile  
 
 Post subject: Re: retrieve object by 2 ids
PostPosted: Mon Jul 20, 2009 5:28 pm 
Newbie

Joined: Mon Jul 20, 2009 5:25 pm
Posts: 4
I think you may need to do a couple of joins between your objects in the query.

http://nhforge.org/doc/nh/en/index.html#queryhql-joins


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