-->
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.  [ 7 posts ] 
Author Message
 Post subject: Concat in Like search
PostPosted: Thu Mar 27, 2008 1:08 pm 
Beginner
Beginner

Joined: Sun Aug 12, 2007 11:22 am
Posts: 44
Location: Sweden
I'm using the Criteria API for my search. I can not make a simple concat to work.

A user must be able to search for an item name. The item name is made up of the brand name and a item name.

I want to concat the brand.name property with item.name property.

ex
Code:
criteria.Add( NHibernate.Expression.Expression.Like( Concat( Brand.Name, Item.Name ) , "%search%" ) );


can this be done?


Top
 Profile  
 
 Post subject: Concat in Like search
PostPosted: Thu Mar 27, 2008 2:22 pm 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi Mathias,

If you add an extra property (it can be protected, and it doesn't actually need to do anything - although it's nicer if it's genuine) to your class:
Code:
protected virtual string BrandItemName
{
    get { return _brandItemName; }
    set { _brandItemName = value; }
}


Then add to your mapping of the class:
Code:
<property name="BrandItemName" formula="BrandName + ItemName" />


Alternatively, when you are saving your item objects, persist a genuine BrandItemName protected property so that you can search on it later. (I would tend towards this option personally)

Then you can use the criteria:
Code:
.Add(Expression.Like("BrandItemName", "%search%"))


Does that help solve your problem?

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 27, 2008 5:14 pm 
Beginner
Beginner

Joined: Sun Aug 12, 2007 11:22 am
Posts: 44
Location: Sweden
I get error when trying to execute

this is a simple example just returning the name. What I would like to is Item.Brand.Name + ' ' + Name

Code:
[Property( Formula="Name" )]
        protected virtual string FullName
        {
            get { return fullName; }
            set { fullName = value; }
        }


Code:
criteria.Add(NHibernate.Expression.Expression.Like("it.FullName", "%88%" ));


Top
 Profile  
 
 Post subject: Concat in Like search
PostPosted: Thu Mar 27, 2008 6:25 pm 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi Mathias,

It might help if we knew the error you were getting.

If you want a formula to cross tables, you need to use SQL names (not object properties) notation, so the formula might be something like:
Code:
<property name="FullName" formula="(select b.Name from Brand b where b.Id = Brand_Id) + Name" />


I would still recommend creating a column and map a protected field to it to avoid the formula altogether.

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 27, 2008 6:34 pm 
Beginner
Beginner

Joined: Sun Aug 12, 2007 11:22 am
Posts: 44
Location: Sweden
Quote:
I would still recommend creating a column and map a protected field to it to avoid the formula altogether.


Can you please tell me what you mean here? Create another column in the table and have a setter and a getter? Can this be done without a setter?

I would like to just have

Code:
protected String FullName{
  get{
     return Item.Brand.Name + " " + Name;
  }
}


Top
 Profile  
 
 Post subject: Concat in Like search
PostPosted: Thu Mar 27, 2008 6:45 pm 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Yes, exactly - with an extra column in the database.

I think you need a setter to keep NHibernate happy, so if you want the property to be public, something like:

Code:
public string FullName
{
    get { return Item.Brand.Name + " " + Name; }
    protected set { // does nothing ... just here for NH }
}


The only problem you might have to watch for is if Brand names change a lot, then the changes have to be cascaded to the Items.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 27, 2008 7:04 pm 
Beginner
Beginner

Joined: Sun Aug 12, 2007 11:22 am
Posts: 44
Location: Sweden
Ok!

I have made the FullName. How ever. If a brand changed I need to go thru all items that has this brand and update the translation. This was not what I was planning but since this is the best alternative I'll have to go with this.

Thanks!


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