View RSS Feed

Hibernate

Hibernate object-relational mapping (ORM) library

  1. Hibernate Queries

    by , 11-07-2011 at 07:02 PM (My Java Tips)
    Hibernate is a popular OR (Object Relational) mapping framework. To access database, we have to use Hibernate Query language. I will present the basics in this post that will help you starting query database tables using HQL.


    Hibernate queries can be roughly divided into 3 categories:

    The queries we general use can be divided up into three main categories :

    - Queries using one class
    - Queries which several classes so join operation is required ...
    Categories
    Hibernate
  2. Jboss-4.0.2 hibernate bug

    by , 11-06-2011 at 05:21 PM (My Java Tips)
    Hibernate is a popular persistence engine that runs in almost any application server and provides alternative of standard entity beans. With JBoss, you can choose to deploy your application as a Hibernate archive, called a HAR file. This makes using hibernate simpler. JBoss is responsible for managing hibernate session and other configuration details.


    Jboss-4.0.2 does not include Hibernate3 integration module. This requires Apache commons collections JAR. To fix this, use fix-hibernate ...
    Categories
    Hibernate , Java EE , JBoss
  3. Creating a Hibernate archive

    by , 11-06-2011 at 05:20 PM (My Java Tips)
    Consider a simple POJO with no direct coupling to Hibernate. The details of the Hibernate mapping will be specified in the Player.hbm.xml file.




    Player.hbm.xml is shown below.

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    <class name="org.jboss.roster.Player" table="PlayerBean">
    <id ...
    Categories
    Hibernate , XML
  4. Using the hibernate objects

    by , 11-06-2011 at 05:18 PM (My Java Tips)
    I assume that you know how to create hibernate archive and you have created a fully configured SessionFactory for use in other parts of your application. I will present an simple JSP which creates a Session from the SessionFactory and issues a query directly.




    Itís a good practice to put the Hibernate access code in a servlet or in a
    session bean, but for example purposes I will use JSP.

    The example shows how to access Hibernate session in the ...
    Categories
    Hibernate
  5. Broken Pipe - Transaction

    by , 10-31-2011 at 04:51 PM (My Java Tips)
    I assume that you are having a J2EE environment with connection pool and you are using hibernate.


    When you use transactions, you have to explicitly commit or rollback every time. Some programmers believe that queries do not have to be committed. This is not right in my opinion. If you do not commit, the connection is left assigned. Which means we have dangling connections that later will be killed by the database server like: MySQL. Connection pool would later attempt to reattach ...
  6. Hibernate Session

    by , 10-31-2011 at 04:44 PM (My Java Tips)
    Declaring your hibernate session as static is not a good idea because a session/connection does not have an endless lifetime.


    If you have static hibernate session, change it to dynamic. Everytime you want to run one or more transactions at once, obtain a new session object from sessionfactory. The given code shows
    static hibernate session. A new session will be created when getSession() is called for the first time. In the later calls, the same session will be returned. ...
    Categories
    Hibernate
  7. Setting up hibernate.cfg.xml

    by , 10-30-2011 at 05:50 PM (My Java Tips)
    Hibernate.cfg.xml is a configuration file that is put in the project's root directory and it has to be put into class path as well.


    A sample cfg file for MySQL is given below. Do change the database URL, database name, username and password.

    <!--?xml version='1.0' encoding='UTF-8'?-->
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt; ...
    Categories
    Hibernate
  8. Hibernate - Mapping files

    by , 10-30-2011 at 05:45 PM (My Java Tips)
    Mapping files are there to map entities with the database tables. In this post, I will present 2 sample mapping files.


    Mapping file for MySql is given below:
    <pre lang="java">
    <!--?xml version="1.0" encoding="UTF-8"?-->

    <hibernate-mapping>
    <class name="com.domain.example.Student" table="student">
    <id name="id" column="id" type="java.lang.Integer"> ...
    Categories
    Hibernate
  9. Hibernate - SessionFactory

    by , 10-29-2011 at 07:44 PM (My Java Tips)
    We are writing an application that will use Hibernate to manage our application persistence object. We will need a SessionFactory which creates or open a session to talk to a database.


    Creating SessionFactory is easy. We can define the configuration in hibernate.properties, hibernate.cfg.xml or create it programatically. I will present an example that will use the hibernate.cfg.xml configuration file, which is mostly use when creating Hibernate application.

    Below is ...

    Updated 10-29-2011 at 07:47 PM by Java Tip

    Categories
    Hibernate
  10. Working with XML data

    by , 10-25-2011 at 06:59 PM (My Java Tips)
    Hibernate allows us to work with persistent XML data same as POJOs. A parsed XML tree is actually a representation of relational data at the object level, instead of POJOs.




    Hibernate uses dom4j API for manipulating XML trees. We can write queries that retrieve dom4j trees from the database and then all modifications to the tree automatically are synchronized to the database.

    One can take an XML document, parse it using dom4j, and write it to the database ...
    Categories
    Hibernate
  11. Hibernate - Fetching strategies

    by , 10-25-2011 at 06:49 PM (My Java Tips)
    Hibernate uses fetching strategy for retrieving associated objects when an application needs to navigate the association. Fetch strategies are declared in the object relational mapping metadata, or over-ridden by a particular HQL or Criteria query.




    Hibernate defines following fetching strategies:

    - Join fetching is used to retrieve the associated instance or collection in the same SELECT, using an OUTER JOIN.

    - Select fetching is used ...
    Categories
    Hibernate
  12. Hibernate Filters

    by , 10-21-2011 at 07:40 PM (My Java Tips)
    A Hibernate filter is a global, named, parameterized filter that may be enabled or disabled for a particular Hibernate session. Filters can be used like database views.




    Filters were introduced in Hibernate3. Using filters, you can define filter criteria and attach those filters at class and a collection level. A filter criteria is actually defining a restriction clause. This is same as "where" attribute available on the class and various collection elements. ...
    Categories
    Hibernate , XML
  13. Lazy associations

    by , 06-02-2011 at 07:26 PM (My Java Tips)
    Hibernate uses lazy select fetching by default for collections and lazy proxy fetching for single-valued associations. This default behavior makes sense for almost all associations in almost all applications.

    Hibernate will use the batch fetch optimization for lazy fetching if hibernate.default_batch_fetch_size parameter is set. This optimization may also be enabled at a more granular level.

    Lazy fetching introduces a problem that should be taken care of. Remember that ...
  14. Criteria instance

    by , 06-02-2011 at 07:22 PM (My Java Tips)
    Hibernate provides an interface org.hibernate.Criteria, which represents a query against a particular persistent class.

    Review the following example:

    Java Code:
    Criteria crit = session.createCriteria(Student.class);
    crit.setMaxResults(50);
    List students = crit.list();
    Resultset can be narrowed according to requirement. The class org.hibernate.criterion.Restrictions defines factory methods for obtaining certain built-in Criterion types. ...
    Categories
    Hibernate
  15. one-to-many, sql

    by , 06-02-2011 at 07:20 PM (My Java Tips)
    Let me presnet a simple <one-to-many> association from Parent to Child.

    XML Code:
    <set name="children">
    <key column="parent_id"/>
    <one-to-many class="Child"/>
    </set>
    Assume that we wish to execute the following code:

    Java Code:
    Parent p = .....;
    Child c = new Child();
    p.getChildren().add(c);
    session.save(c);
    session.flush();
    ...
    Tags: one-to-many Add / Edit Tags
    Categories
    Hibernate
  16. No Persistence provider for EntityManager

    by , 06-02-2011 at 07:15 PM (My Java Tips)
    You might have encountered the following exception while working with hibernate:

    Java Code:
    [java] Exception in thread "main" javax.persistence.PersistenceException: 
    No Persistence provider for EntityManager named  ****
    Make sure you have all the required jars in the class path.

    Java Code:
          \antlr-2.7.6.jar
          \asm.jar
          \asm-attrs.jar
          \c3p0-0.9.0.jar
          \cglib-2.1.3.jar
    ...
  17. hibernate.connection.rele ase_mode

    by , 05-25-2011 at 09:49 AM (My Java Tips)
    Hibernate JDBC/Connection's property hibernate.connection.release_mode is used to specify when Hibernate should release JDBC connections.

    A JDBC connection is held until the session is explicitly closed or disconnected by default. For a JTA datasource, one should use after_statement to aggressively release connections after every JDBC call. And for a non-JTA connection, it often makes sense to release the connection at the end of each transaction. after_transaction.auto will
    ...
    Categories
    Hibernate
  18. Hibernate Dialects

    by , 05-25-2011 at 09:46 AM (My Java Tips)
    To set SQL dialect, one has to set the hibernate.dialect property to correct org.hibernate.dialect.Dialect subclass for the database.

    Specify a dialect and then Hibernate will use defaults for some of the properties and this will save the effort of specifying them manually.

    Following dialects are supported.

    • DB2 org.hibernate.dialect.DB2Dialect
    • DB2 AS/400 org.hibernate.dialect.DB2400Dialect
    • DB2 OS390 org.hibernate.dialect.DB2390Dialect
    • PostgreSQL
    ...
  19. SessionFactory Monitoring

    by , 05-21-2011 at 10:37 PM (My Java Tips)
    There are few ways of accessing SessionFactory metrics. One of them is to use JMX to publish metrics. For that you need to enable the StatisticsService MBean. A single MBean can be enabled for your SessionFactory or one per factory.

    Following snippet presents a configuration example:

    Java Code:
    // MBean service registration for a specific SessionFactory
    Hashtable tb = new Hashtable();
    tb.put("type", "statistics");
    tb.put("sessionFactory",
    ...
    Categories
    Hibernate
  20. Hibernate logging

    by , 05-21-2011 at 10:36 PM (My Java Tips)
    Hibernate logs various events using Apache commons-logging. Hibernate logs are very interesting to read if you want to do some troubleshooting or performance improvement. Hibernate uses commons-logging service for logging. It actually sends the log to either Log4j or to JDK1.4 logging.

    If you want hibernate to log using Log4j, then put log4j.jar into class path along with log4j.properties. Since log4j has almost become a standard these days, I would advice using that.
    Hibernate ...
    Tags: hql, jdbc, log4j, logging, sql Add / Edit Tags
    Categories
    Hibernate
  21. Defining fetch strategy

    by , 05-21-2011 at 10:33 PM (My Java Tips)
    To tune fetch strategy, you have various options. Which to choose depends on the scenario. You may define the fetch strategy in the mapping document. For instance:

    XML Code:
    <set name="permissions" 
                fetch="join">
        <key column="userId"/>
        <one-to-many class="Permission"/>
    </set
    <many-to-one name="mother" class="Cat" fetch="join"/>
    ...
    Tags: hql Add / Edit Tags
    Categories
    Hibernate
  22. Lazy select fetching for collections

    by , 05-21-2011 at 10:30 PM (My Java Tips)
    Hibernate uses lazy select fetching for collections by default. This behavior makes sense and is used as it is unless you really want to do something different for some reason.

    Hibernate will use the batch fetch optimization for lazy fetching if you set hibernate.default_batch_fetch_size. There is a problem with lazy fetching that should be considered. Remember, you should never access to a lazy association outside of the context of an open Hibernate session otherwise exceptions ...
    Categories
    Hibernate