-4

I am working on a web application project that require to interact with a database (Oracle 12c in this case):

To understand and test the capabilities of Hibernate well( As I am a first time user of Hibernate): I wrote a small web application that calls a web service. That web service's backend in turn uses hibernate to fetch the data from Oracle Database,( It execute a read only query( a stored procedure via CreateSQLQuery)).

It has given following results, ( where web service running on my local(tomcat 8), and database is in remote (aws)) :

520 Records to Fetch : 21020 ms (Around 21 seconds to fetch )

3846 Records to Fetch : 172383 ms ( 172 Seconds ~ 3 Minutes )

I have couple of questions :

1. I would like to know what is the use of Hibernate ?

2. Its too Slow in my first exercise, and I searched all over internet but didn't find any straight forward answer or directions ?

3. If you are the one who is using hibernate then may you please let me know why you are using this hibernate, and what business problem its solve ( e.g. is it for beginners who can just run basic web application ?)

4. May you please let me know what is missing for a developer from Hibernate?

References : simple-hibernate-query-returning-very-slowly

why-is-the-hibernate-query-list-slow

hibernate-performance

hibernate-query-running-slow

sql-query-within-hibernate-extremely-slow-while-very-fast-using-sql-cient

Below is the context with my simple Hibernate work :

Query :

List<Model> modelList = session.createSQLQuery("SELECT * FROM TABLE)
                .addEntity(modelList.class).list();

*TABLE - I have changed the name for simplicity, overyall I am running only a simple select query to fetch the that, nothing complex,

Config File

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@X.X.X.X:1521:orcl</property>
        <property name="hibernate.connection.password">*****</property>
        <property name="hibernate.connection.username">******</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto"></property>
        <mapping class="x.x.x.x.xsModel" />
        <mapping class="x.x.x.x.dModel" />


    </session-factory> </hibernate-configuration>

HiberNate Util

public class HibernateUtil { //private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory sessionFactory = buildSessionFactory();

private static final Logger logger = LoggerFactory.getLogger(HibernateUtil.class);

private static SessionFactory buildSessionFactory()
{
    try
    {
        if (sessionFactory == null) {
            // loads configuration and mappings
            Configuration configuration = new Configuration().configure()
                    .setProperty("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver")
                    .setProperty("hibernate.connection.url", "jdbc:oracle:thin:@X.X.X.X:orcl")
                    .setProperty("hibernate.connection.password","aaaaa")
                    .setProperty("hibernate.connection.username","aaaaa")
                    .setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect")
                    .setProperty("show_sql","true")
                    //.setProperty("hibernate.hbm2ddl.auto", "")
                   // .setProperty("hibernate.temp.use_jdbc_metadata_defaults","false")
                   // .setProperty("hibernate.jdbc.lob.non_contextual_creation","true")
            .addAnnotatedClass(dModel.class)
                    .addAnnotatedClass(sModel.class);

            ServiceRegistry serviceRegistry
                    = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();


            // builds a session factory from the service registry
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }
        return sessionFactory;
    }
    catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        logger.error("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {

    return sessionFactory;
}

public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
}

}

Community
  • 1
  • 1
Sumit Arora
  • 5,051
  • 7
  • 37
  • 57

2 Answers2

3

1.I would like to know what is the use of Hibernate ?

If you don't know that, then you shouldn't have started using it. Hibernate is an ORM: Object-Relational Mapper. It allows "viewing" rows from various tables of a database as an interconnected graph of objects (called entities). Hibernate deals with transforming rows to objects, lazy-load the associations, track the state of every object and make it persistent transparently, among other things.

The main benefit is to be able to write code that gets and writes data to a database in a more natural, object-oriented way, letting Hibernate do the cumbersome job of querying the database.

  1. Its too Slow in my first exercise, and I searched all over internet but didn't find any straight forward answer or directions ?

Your use-case isn't the best use-case to use Hibernate. You should go with plain JDBC:

  • it uses a single table, so the support for associations and lazy-loading is useless
  • the additional complexity isn't worth it, when you have such a simple use-case
  • Hibernate is indeed slower, unless you follow the instructions in the manual, when dealing with a very large number of entities. That's not what it's made for.

3.If you are the one who is using hibernate then may you please let me know why you are using this hibernate, and what business problem its solve ( e.g. is it for beginners who can just run basic web application ?)

See 1.

4.May you please let me know what is missing for a developer from Hibernate?

See 1.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

An OR/M is overkill for your.project. They are best used for transactional data (load, modify, store). Also, one of the main benefits is not needing to write SQL but you are doing that anyway.

The main reason you are experiencing poor performance is that all the data from every record is being stored in a session cache.

I recommend using the native Oracle client and writing into your objects directly.

Chet
  • 3,461
  • 1
  • 19
  • 24
  • 1
    I have used JDBC and took more or less same time as taken by Hibernate ORM--520 Records to Fetch : 21020 ms (Around 21 seconds to fetch ), 3846 Records to Fetch : 172383 ms ( 172 Seconds ~ 3 Minutes )(In JDBC its just a 1-2 seconds gain on the first one and 20 seconds gain from the second one). What do you mean by Native Oracle Client ? – Sumit Arora Dec 13 '15 at 17:55