views:

277

answers:

1

What is your favourite Hibernate trick?

+3  A: 

In a webapp:

using a Filter to start and terminate a transaction

defining a view to accommodate tomcat's jdbc authentification:

  <database-object>
    <!-- rolemap view -->
    <create>
            CREATE VIEW rolemap(username,role) AS
            SELECT u.username,ur.role_code
            FROM users as u, users_roles as ur
            WHERE ur.user_id=u.id
        </create>
    <drop>DROP VIEW IF EXISTS rolemap</drop>
  </database-object>

casting a query result into a typed list:

Session pm = Factory.getSession();
Query qry = pm.createQuery("from Laboratory");
@SuppressWarnings("unchecked")
List<Laboratory> labs = (List<Laboratory>)qry.list();
for (Laboratory lab: labs) {
    ...
}
Maurice Perry