Hibernate

What is c3p0 in Hibernate ?

c3p0 is used in a Hibernate connection pooling mechanism. To use this c3p0 connection pool you need to put c3p0 jar in the classpath. Next you need to tell Hibernate that you want to use the C3P0 connection pool. You do so by using one or more of the C3P0 properties in the configuration file. The available properties are as follows:

hibernate.c3p0.acquire_increment: The total number of connections C3P0 will create when all connections in the pool have been used. The default value for this property is 3.

hibernate.c3p0.idle_test_period: The time before a connection in the pool is validated against the database. The default value for this property is 0. A value greater than 0 causes c3p0 to test all unused connections every x seconds; where x is the value for this property.

hibernate.c3p0.max_size: The maximum number of connections in the pool. The default value is 15.

hibernate.c3p0.max_statements: The maximum size of the c3p0 statement cache. The default value is 0, meaning the cache is turned off.

hibernate.c3p0.min_size: The minimum number of connections in the pool. The default value is 3.

hibernate.c3p0.timeout: The maximum time a connection will be kept in the pool without being used. The default value is 0, meaning that all connections in the pool won’t expire.

What is DBCP in Hibernate ?

This is an another connection pool mechanism in Hibernate. To use use DBCP you need to put commons-dbcp jar in the class-path.

If you want Hibernate to use DBCP for database connection pooling, you must set one of the following properties:

hibernate.dbcp.maxActive: The total number of connections that can be used from the pool at any given time. The default is 8.

hibernate.dbcp.maxIdle: The maximum number of connections that can remain in the pool at any given time. The default is 8.

hibernate.dbcp.maxWait: The number of milliseconds a connection can be used from the pool. The default value of -1 means indefinitely.

hibernate.dbcp.testOnBorrow: Specifies whether a connection is validated before being taken out of the pool. The default is true.

hibernate.dbcp.testOnReturn: Determines whether a connection is validated upon return from the application. The default is false.

hibernate.dbcp.validationQuery: The query string used to validate the connection.

 What is Proxool in Hibernate ?

This is an another connection pool mechanism in Hibernate. To use use DBCP you need to put proxool jar in the classpath.

To activate Proxool in Hibernate, set one of the following properties.

hibernate.proxool.existing_pool: Determines whether the current pool should be configured from an existing pool. Possible values are true and false.

hibernate.proxool.pool_alias: An alias to use with the Proxool pool. You need to use this value, because it will be referenced in the existing pool, properties, or XML file.

hibernate.proxool.properties: The path to the proxool.properties property file.

hibernate.proxool.xml: The path to the proxool.xml property file.

How does Hibernate allow user-based connections ?

try {
    Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/roytuts");
    SessionFactory sessionFactory = Configuration.buildSessionFactory();
    Session session = sessionFactory.openSession(connection);
} catch (Exception e) {}

How to connect multiple database servers using Hibernate ?

Please refer to the tutorial here at Connect to multiple database servers using Hibernate

What is mapping document in Hibernate ?

A mapping document is used to map database table column to Java class. Hibernate can’t persist Java objects without a mapping document.

You can also use Java based configuration and Java entity classes to remove completely XML based configurations.

The format of the mapping document is:

<?xml version=" 1.0" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd>
    <hibernate-mapping>
    </hibernate-mapping>

What is default-cascade in Hibernate mapping document ?

Specifies how actions that are performed on a parent are cascaded to a child. The parent/child relationship is created through one-to-one, many-to-many, or one-to-many mappings.

For each mapping, you can specify a cascade attribute; if you don’t, the default-cascade attribute’s value is used.

There are four possible values for default-cascade: all, none, save-update, and delete. If you do not specify a default-cascade attribute, the default is none (no cascading of options occurs).

What is mutable attribute in class element in Hibernate mapping document ?

It signals whether an object persisted by Hibernate can be updated or deleted. If this attribute is set to false, then the object is essentially read-only. The object can be persisted by Hibernate but never updated into or deleted from permanent storage.

What is proxy attribute in class element in Hibernate mapping document ?

It tells Hibernate whether to use lazy initialization for objects of this mapping type. Lazy initialization lets Hibernate stub out the data in an object. If you’re using lazy initialization, you create objects and load them but Hibernate won’t populate (make a call to the database for data) until the object is used. The proxy attribute requires that you provide a class to be used for the lazy initialization; this should normally be the class name used in the name attribute.

What is dynamic-update attribute in class element in Hibernate mapping document ?

If dynamic-update is set to true, then when Hibernate updates a row in the database for a class attribute that has changed, it generates an UPDATE SQL statement at runtime during the update() method call on the session and only includes the columns of the table that have been updated. This is an optional attribute; the default value is false.

What is dynamic-insert attribute in class element in Hibernate mapping document ?

This is same as dynamic-update in principle; but Hibernate dynamically creates an INSERT SQL statement with only those columns that do not have null values. This is an optional attribute; the default value is false.

What is select-before-update attribute in class element in Hibernate mapping document ?

If it is set to true, then Hibernate performs a select operation to determine if an update is needed (by default, Hibernate won’t perform an update unless the object has been modified). This is an optional attribute; the default is false.