-->
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.  [ 1 post ] 
Author Message
 Post subject: Register function error - complex order by clause
PostPosted: Wed Jun 17, 2009 4:21 pm 
Newbie

Joined: Tue Feb 03, 2009 2:49 pm
Posts: 4
I need a complex order by, because the field to order by is not in the same table of the main entity. So I tried to register a function that returns that field.

These are the classes involved:

Code:
.....
@Table(name = "product")
public class ProductImpl extends RelationableImpl implements Product {

   private Map<String, Attribute> attributes = new HashMap<String, Attribute>();

   @OneToMany(targetEntity = AttributeImpl.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   @MapKey(name = "name")
   public Map<String, Attribute> getAttributes() {
      return attributes;
   }

   public void setAttributes(Map<String, Attribute> attributes) {
      this.attributes = attributes;
   }
....
}
......
@Table(name = "attribute")
public class AttributeImpl extends BaseObjectImpl implements Attribute {

   private String name;
   private String value;
............... (get and set)
}


I've registered this function in my own MySql dialect:

Code:
......
registerFunction(
            "get_dynamic_attribute",
            new SQLFunctionTemplate(
                  Hibernate.STRING,
                  "SELECT att.VALUE FROM attribute AS att WHERE att.id IN (SELECT attributes_id FROM product_attribute WHERE product_id=?1) and att.name=?2"));


And I called the function above with this hql:

Code:
"select distinct............. order by get_dynamic_attribute(product.id,'vendor_part') asc"


I want s list of product order by a dynamic attribute called vendor_part, but when I run the code above I get this exception:

Code:
.......
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
.......
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT att.VALUE FROM attribute AS att WHERE att.id IN (SELECT attributes_id FRO' at line 1
.......


Anybody can help me and tell me what I'm doing wrong please??

Or, what can I do to get a list of products order in this way??

_________________
Leandro Iriarte. Systems Engineer
http://www.leandroiriarte.com.ar


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.