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.