Announcement Announcement Module
No announcement yet.
Roo and Hibernate Criteria Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Roo and Hibernate Criteria

    I am using Roo with JPA and want to build custom SQL queries in the controller. Rather than building up a string based query it would be nice to use Hibernate Criteria (or something similar) to build the query. I know roo uses Hibernate underneath so I thought this would be possible.

    Thought his might work: (org.hibernate.Session)entityManager.getDelegate() , but it didn't.

    Any ideas how to access hibernate criteria or is it not possible?

  • #2
    You should use the same technique you use when accessing Hibernate with JPA, Roo don´t uses only Hibernate internally, it uses any JPA provider specified, but there´s nothing diferent from using hibernate in a Roo´s application than using it in a non roo´s application.

    I don´t know exactly how could you access the hibernate API from JPA, but the fact is that you don´t need to worry about Roo in the process, Roo´s architecture is completely non invasive, and Roo applications are standard spring /JPA applications



    • #3
      As Raul mentioned, Roo makes use of JPA for its ORM needs. This allows for a nice abstraction giving you the choice between three JPA implementations: Hibernate, EclipseLink and OpenJPA. Now, if you want to create custom queries all you need to do is get hold of the EntityManager in your controller (or wherever you need it):

      private EntityManager entityManager;
      This should get it injected into any Spring managed component (or any Pojo marked with @Configurable.

      There is some abstraction available in the JPA API to build standard queries for you but it is probably not as powerful compared to using Hibernate directly. We are hoping to convert to JPA 2 soon which should help on that.



      • #4
        If you really want or need to break the abstraction layer and bypass JPA

        ((Session)entityManager().getDelegate()).createCri teria(MyObject.class);

        seems to work fine for me on tomcat.

        It seems the behavior may be different depending on your application server? Eg. Glassfish vs. JBoss as described here: