-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate generate select while storing object.
PostPosted: Fri Dec 12, 2008 11:22 am 
Newbie

Joined: Thu Dec 04, 2008 7:00 am
Posts: 4
Hi,
Here we are using JTA transaction and Ehcache, first we are taking TransactionManager instance to manage the transaction across
different sessions, then we have single instance of SessionFactory instance through which we are taking instances of Session Object,
here we are taking Different Session object to store each object , and after storing each object we flush and close the session.
First we store all dependent object first and then store other object, but what hibernate does is that , when we go for storing other object it applies the
select query for dependent object even though they stored before, so it cause the performance problem, so if any one can help us in overcome this
issue? for example below i have mentioned my sample code and configuration part.


I.E.
-> here we are initiating common SessionFactory instance
public static final SessionFactory sessionFactory;
static {
try{
// Create the SessionFactory
Configuration configuration = new Configuration();
//configuration.setInterceptor(new AsiteInterceptor());
sessionFactory = configuration.configure().buildSessionFactory();
}catch(Throwable ex){
// Make sure you log the exception, as it might be swallowed
throw new ExceptionInInitializerError(ex);
}
}

-> initiating Transaction manager and being the transaction.

InitialContext ctx = new InitialContext();
TransactionManager tm = (TransactionManager)ctx.lookup("java:/TransactionManager");
tm.begin();
Transaction ts = tm.getTransaction();

-> Opem new Session object from SessionFactory and stroing instance of ClassC, and closing the session after storing.
session = HibernateUtil.getSessionFactory().openSession();
session.save(objc);
session.flush();
session.close();

-> creating another Session object from same SessionFactory and stroing instance of ClassB which has references of above object

session = HibernateUtil.getSessionFactory().openSession();
//session.setCacheMode(CacheMode.NORMAL); //does not effect the outcome.
session.save(objb);
ession.flush();
session.close();

so what the problem us while storing this object in DB it applies select for it referenc object of ClassC

-> now how we configure Ehcache in our application
hibernate.hb.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

<!-- <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://dbbox:1433;DatabaseName=AsiteModelServer</property>
<property name="connection.username">dmsdb</property>
<property name="connection.password">dmsdb</property>
-->

<property name="connection.pool_size">10</property>
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">true</property>

<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>

<property name="hibernate.connection.datasource">java:AmsDS</property>
<property name="current_session_context_class">jta</property>
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>

<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_minimal_puts">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>


<mapping resource="mapping/classb.hbm.xml"/>

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

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

<hibernate-mapping>

<class name="com.test.ClassB" table="ClassB">
<cache usage="read-only"/>
<id name="id">
<generator class="assigned"/>
</id>
<property name="bdata"/>
<many-to-one name="classc" class="com.test.ClassC" />
</class>

<class name="com.test.ClassC" table="ClassC">
<cache usage="read-only"/>
<id name="id">
<generator class="assigned"/>
</id>
</class>
</hibernate-mapping>

ehcache.xml

<ehcache>
<cache name="com.test.ClassB"
maxElementsInMemory="450"
eternal="false"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<cache name="com.test.ClassC"
maxElementsInMemory="450"
eternal="false"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
</ehcache>

SQL log for this storing

20:24:37,109 INFO [STDOUT] Hibernate: insert into ClassC (id) values (?)
20:24:37,140 INFO [STDOUT] Hibernate: select classc_.id from ClassC classc_ where classc_.id=?
20:24:37,140 INFO [STDOUT] Hibernate: insert into ClassB (bdata, classc, id) values (?, ?, ?)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.