-->
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.  [ 3 posts ] 
Author Message
 Post subject: Help with Using JOIN - and OrderBy
PostPosted: Thu Jun 05, 2008 8:44 am 
Newbie

Joined: Tue Apr 22, 2008 8:29 am
Posts: 3
I have two tables: Menu and MenuName.

+---------------+
| Menu
+---------------+
| seq_no (pk)
| n_id (pfk)
| ..
| ..
+---------------+
V
|
|
+---------------+
| Name
+---------------+
| n_id (pk)
| n_name
| ..
+---------------+

Where Name.n_id is part of the composite primary key for Menu. I am trying to create a Criteria query where the results will be ordered by the n_name field.

The Native SQL query is

Code:
select m.n_id, n.n_name, m.seq_no
from menu m, name d
where m.n_id=n.n_id
order by n.n_name


which works.

The following code:

Code:

class MenuName {
   int id;
   string name;

   Set menus;
}

class Menu {
   pivate MenuPK primaryKey
}

class MenuPK {
  int seq;
  MenuName menuname;
}

// mapping snippet for Menu (some attributes omitted)

<class name="Menu">
  <composite-id name="PrimaryKey" class="MenuPK">
    <key-property name="seq" .. />
    <key-many-to-one name="menuname" class="MenuName" .. />
  </composite-id>
...
</class>

<class name="Menu">
  ..
  <propery name="name" ... />
  <set name="menus">
    <one-to-many class="Menu" .. /> 
  </set>
</class>




I tried the following and get error claiming the property "name" cannot be found.
Code:
Criteria c = session.createCriteria(Menu.class)
                   .createAlias("PrimaryKey.menuname", "nm", CriteriaSpecification.INNER_JOIN)
                   .add(Restrictions.like("nm.name", "%Main Menu%"))
                   .addOrder(Order.asc("nm.name"))


It looks like the two tables are not being joined as anticipated.

Am I missing something?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 11:37 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Is it returning nm.name not found, or just name? I'm wondering if its an alias problem?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 11:42 am 
Newbie

Joined: Tue Apr 22, 2008 8:29 am
Posts: 3
The underlying error is the following:

ORA-00904: "nm_"."n_name": invalid identifier

When looking at the generated SQL, there isn't a join clause.


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