Results 1 to 4 of 4

Thread: Roo and Hibernate Criteria

  1. #1
    Join Date
    Nov 2009
    Posts
    8

    Post 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. #2

    Default

    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

    HTH

  3. #3
    Join Date
    Mar 2008
    Location
    Sydney, AU
    Posts
    974

    Default

    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):

    Code:
    @PersistenceContext    
    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.

    Cheers,
    Stefan

  4. #4
    Join Date
    Jan 2010
    Posts
    4

    Default

    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: http://weblogs.java.net/blog/2009/05...gergetdelegate

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •