How do I accomplish this custom sort by field feature available in MySQL in hibernate?
select * from pet order by field(species, 'cat', 'dog', 'bird');
For some business reason, I need to enforce a custom ordering.
PS -I am new to hibernate.
How do I accomplish this custom sort by field feature available in MySQL in hibernate?
select * from pet order by field(species, 'cat', 'dog', 'bird');
For some business reason, I need to enforce a custom ordering.
PS -I am new to hibernate.
I ended up writing an HQL like this
commaDelimitedSpecies = "'cat', 'dog', 'bird'";
orderBySpecies = " ORDER BY FIELD(species, " + commaDelimitedSpecies + ") DESC";
Query q = getSession().createQuery("FROM PetModel pet" + orderBySpecies);
With Spring HibernateDAO:
getHibernateTemplate().findByNamedParametersQuery(
"FROM MyEntity me WHERE me.id IN :ids ORDER BY FIELD(id, :ids)", new String[] {"ids"}, new Object[] {uuidList.toArray()},uuidList.size()
);
Note: getHibernateTemplate returns an instance of org.springframework.orm.hibernate3.HibernateTemplate