Hibernate Query Articles

List of Hibernate Query Articles   How to see the SQL generated by Hibernate Hibernate and JPA named query example Hibernate Query by example How to open an online cursor with Hibernate? Using native Queries with Hibernate and JPA How to call a stored procedure with Hibernate ? How to paginate your Entity How to … Read more

How to see the SQL generated by Hibernate

Hibernate generates SQL statements that let you access the database transparentely. You can set the show_sql property to true in the hibernate.cfg.xml configuration file to print the SQL statements to the console: <property name=”show_sql”>true</property>

Hibernate and JPA named query example

Hibernate and JPA specification lets you externalize query strings to the mapping metadata, a technique that is called named queries. This allows you to store all queries related to a particular persistent class (or a set of classes) in an XML mapping file or using some simple annotations.    In any case, The name of … Read more

Hibernate Query by example

Hibernate query by example Retrieving objects from the database is one of the most interesting parts of Hibernate.Hibernate provides the following ways to get objects out of the database: Retrieval by identifier, the most convenient method when the unique identifier value of an object is known. The Hibernate Query Language (HQL), which is a full … Read more

How to open an online cursor with Hibernate?

Supposing you have to fetch a large resultset and update the single objects. By using the standard Query Object you would retrieve the whole set of Objects in Memory: Query q = session.createQuery(“from ABC”); List l = q.list(); If you need to operate on an online cursor, then you can use the ScrollableResults: Session session … Read more

Using native Queries with Hibernate and JPA

What are your options if your Hibernate/EJB application has to deal with a very complex and DB-optimized SQL query ? good interview question….Hibernate has a built-in object oriented language for querying objects called HQL. This language is quite complete and efficient, so in theory you could translate every SQL query in its equivalent HQL ( and … Read more

How to call a stored procedure with Hibernate ?

Supposing you have the following Oracle Stored procedure: CREATE FUNCTION agenda RETURN SYS_REFCURSOR AS      my_cursor SYS_REFCURSOR; BEGIN     OPEN my_cursor FOR         SELECT id,person_name,person_surname,person_address         FROM Person;     RETURN  my_cursor;  END; Invoking it from Hibernate requires mapping the Stored Procedure in the Person class. <sql-query name=”SP_agenda” callable=”true”>     <return alias=”ev” class=”Person”>         <return-property name=”id” column=”id”/> … Read more

How to paginate your Entity

Retrieving a large amount of data and returning them to the client can be quite paniful….however with EJB 3 it’s quite easy to paginate a large resultset: for example supposing you want to retrieve only the first “page” of 100 Users:   List <Users> tasklist = em.createNamedQuery(“findUsersByRole”)     .setParameter(“role”, role)     .setMaxResults(100)     .setFirstResult(0)     .getResultList(); You could further … Read more

How to use native queries with EJB 3.0 ?

JBoss recipe of the day You can use the createNativeQuery method of the EntityManager interface to create a dynamic query using SQL as follows:   Query q = em.createNativeQuery(“SELECT p1.last_name, t1.title_name, t2.title_name FROM person p1 INNER JOIN title t1 ON p1.title_1 = t1.title_abbrev, person p2 INNER JOIN title t2 ON p2.title_2 = t2.title_abbrev”, sample.Person.User.class); return … Read more

How to limit the number of records in a Query with EJB 3.0 ?

In order to limit the number of records returned by a Query, you can use the setMaxResults method from the’ Java persistence API’ Query Example: @PersistenceContext(unitName=”myPersistenceUnit”) ‘ ‘  private EntityManager em; ‘ ‘  private int’ maxRecords = 100; ‘ ‘  ‘ ‘  public void getNextItems() { ‘ ‘ ‘ ‘ ‘ ‘ ‘  Query q = em.createQuery(“select object(o) from Item as o”); ‘ ‘ ‘ ‘ ‘ ‘ ‘  q.setMaxResults(maxRecords); ‘ ‘ ‘ ‘ ‘ ‘ ‘  items= q.getResultList(); ‘ ‘  … Read more