Joining tables in SQL is the foundation of a relational database, as joins allow you to actually model relationships between tables. In plain JDBC, joins between tables are composed using native SQL statements. On the other hand, when using Hibernate/JPA this is greatly simplified as you will specify the relation between tables in terms of Objects. A key aspect of it is that, when you define a relationship in an ORM tool like Hibernate or JPA, you’ll also need to define the fetch type.

0
0
0
s2smodern

Providing a connection pool for an application that uses Hibernate is pretty easy, as a matter of fact Hibernate supports a variety of connection pooling mechanisms. If you are using an application server, you may wish to use the built-in pool (typically a connection is obtaining using JNDI).

For example, this is a sample hibernate.cfg.xml configuration file that is used to handle connections to a MySQL database which is bound into the JNDI as java:jboss/datasources/MySqlDS

<hibernate-configuration>
    <session-factory>
            <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="connection.datasource">java:jboss/datasources/MySqlDS</property>
    </session-factory>
</hibernate-configuration>

If you can't or don't wish to use your application server's built-in connection pool, Hibernate supports several other connection pools such as:

c3p0

http://sourceforge.net/projects/c3p0

Distributed with Hibernate

Apache DBCP

http://jakarta.apache.org/commons/dbcp/

Apache Pool

Proxool

http://proxool.sourceforge.net/

Distributed with Hibernate

 

Configure C3P0 Connection Pool

Here's a sample configuration for the built-in Connection pool:


<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myschema</property>
  <property name="hibernate.connection.username">user</property>
  <property name="hibernate.connection.password">password</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">true</property>
 
  <property name="hibernate.c3p0.min_size">5</property>
  <property name="hibernate.c3p0.max_size">20</property>
  <property name="hibernate.c3p0.timeout">300</property>
  <property name="hibernate.c3p0.max_statements">50</property>
  <property name="hibernate.c3p0.idle_test_period">3000</property>
 
  . . . .
</session-factory>

And some explanation about the properties:

hibernate.c3p0.min_size – Minimum number of JDBC connections in the pool. Hibernate default: 1
hibernate.c3p0.max_size – Maximum number of JDBC connections in the pool. Hibernate default: 100
hibernate.c3p0.timeout – When an idle connection is removed from the pool (in second). Hibernate default: 0, never expire.
hibernate.c3p0.max_statements – Number of prepared statements will be cached. Increase performance. Hibernate default: 0 , caching is disable.
hibernate.c3p0.idle_test_period – idle time in seconds before a connection is automatically validated. Hibernate default: 0

Configure Apache DBCP Connection Pool

Apache Connection Pool can be downloaded from http://commons.apache.org/dbcp/

In order to integrate this pool with Hibernate you will need the following jars: commons-dbcp.jar and commons-pool-1.5.4.jar.

Here's a sample configuration in hibernate.cfg.xml:


<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myschema</property>
  <property name="hibernate.connection.username">user</property>
  <property name="hibernate.connection.password">password</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">true</property>
 
  <property name="hibernate.dbcp.initialSize">8</property>
  <property name="hibernate.dbcp.maxActive">20</property>
  <property name="hibernate.dbcp.maxIdle">20</property>
  <property name="hibernate.dbcp.minIdle">0</property>
 
  . . . .
</session-factory>

0
0
0
s2smodern

Hibernate archives can be deployed as top level packages or can be deployed as a component of an EAR file. Since Hibernate archives are not a standard J2EE deployment type, we need to declare them in the jboss-app.xml file of an EAR file to use them that context.

This is done with a module/har element as shown in the following example.

<!DOCTYPE jboss-app PUBLIC "-//JBoss//DTD J2EE Application 1.4//EN"
"http://www.jboss.org/j2ee/dtd/jboss-app_4_0.dtd">
 <jboss-app>
  <module>
   <har>sample.har</har>
  </module>
 </jboss-app>

With this, a Hibernate archive can be deployed along side a WAR or EAR file in any EAR file.

0
0
0
s2smodern

You can enable live statistics by setting the property hibernate.generate_statistics in theconfiguration file:


<property name="hibernate.generate_statistics">true</property>

It is also possible to enable live statistics programmatically using the Statistics Interface:


Statistics stats = sessionFactory.getStatistics();

stats.setStatisticsEnabled(true);

Transaction tx = session.beginTransaction();

List<Customer> c = session.createQuery("from Customer").list();

for(Customer c : customers){

System.out.println(c);

}

stats.getSessionOpenCount();

stats.logSummary();

session.close();

0
0
0
s2smodern