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
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();
}
}