-->
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.  [ 8 posts ] 
Author Message
 Post subject: hibernate integrate with jdbc
PostPosted: Mon Jun 18, 2012 10:29 am 
Newbie

Joined: Sat Jun 16, 2012 1:41 am
Posts: 8
Hi all,
Hibernate is a good tool. But in my project, some query is so complex, so I decided to use SQL directly for these query. I am using Hibernate 3.6.10. It provides a Work interface, we can execute my sql inside the doWork() method of Session interface. But I found my sql query return nothing even thought I added a record just before this query using Hibernate session save() method. I check hibernate source codes, and found no flush occurs before Work execution. So, I think I must call session flush() method manually before executing my sql query. Flush frequently will cause performance issues. So my question is if hibernate provide a graceful method for user to execute sql query directly and keep the sql query consistent with hibernate session?


Top
 Profile  
 
 Post subject: Re: hibernate integrate with jdbc
PostPosted: Mon Jun 18, 2012 11:23 am 
Newbie

Joined: Sat Jun 16, 2012 1:41 am
Posts: 8
Can anyone give some advice?


Top
 Profile  
 
 Post subject: Re: hibernate integrate with jdbc
PostPosted: Mon Jun 18, 2012 11:48 am 
Newbie

Joined: Thu Jan 27, 2011 10:53 am
Posts: 12
Hello,
Have a look at that link:
http://www.mkyong.com/hibernate/hiberna ... -examples/
Hope it helps.


Top
 Profile  
 
 Post subject: Re: hibernate integrate with jdbc
PostPosted: Mon Jun 18, 2012 12:30 pm 
Newbie

Joined: Sat Jun 16, 2012 1:41 am
Posts: 8
Thanks fsouleil. Your reply is so important for me. I have checked the source codes, the native sql can resolve my problem.


Top
 Profile  
 
 Post subject: Re: hibernate integrate with jdbc
PostPosted: Tue Jun 19, 2012 12:07 am 
Newbie

Joined: Sat Jun 16, 2012 1:41 am
Posts: 8
I had tested this case again, and I found hibernate could not flush the add action to the database. My code is as below:
Code:
Quotation quotation = new Quotation();
quotation.setQuotationId(xx);
quotation.setAmount(xx);
......
quotation.add();

resultLst = HibernateUtil.currentSession().getNamedQuery("Quotation_query")
            .setInteger("qid", quotation.getQuotationId())
            .list();
         if (resultLst != null && !resultLst.isEmpty()) {
            for (Object[] objs : resultLst) {
               System.out.println(objs[1]);
            }
         }


The above getNamedQuery() method return a empty list. I don't know why hibernate does not flush the insert action to the database.


Top
 Profile  
 
 Post subject: Re: hibernate integrate with jdbc
PostPosted: Tue Jun 19, 2012 11:22 am 
Newbie

Joined: Sat Jun 16, 2012 1:41 am
Posts: 8
Any one has good ideas?


Top
 Profile  
 
 Post subject: Re: hibernate integrate with jdbc
PostPosted: Tue Jun 26, 2012 7:55 am 
Newbie

Joined: Thu Jan 27, 2011 10:53 am
Posts: 12
I guess you've got trouble with the first level cache.
You can try:
- Flush session explicitidly
- Implement hashcode and equals method for your bo Quotation based on a functional key (not the id field)
Regards


Top
 Profile  
 
 Post subject: Re: hibernate integrate with jdbc
PostPosted: Wed Jun 27, 2012 11:15 am 
Newbie

Joined: Sat Jun 16, 2012 1:41 am
Posts: 8
Hi fsouleil, thanks for your reply. I had addressed this issue by specifying the flush-mode of the sql-query, just like following:
Code:
<sql-query name="Quotation_query" flush-mode="always">
      <return-scalar column="id" type="integer"/>
      <return-scalar column="name" type="string"/>
      <![CDATA[
      select q.quotationId as id, q.quotationName as name from quotation q where q.quotationId = :qid
      ]]>
</sql-query>


When I tracked the source codes, I found hibernate use quereySpaces and flush-mode to decide whether to flush before sql-query execution or not. But I could not find where to specify the querySpaces for sql-query. sql-query could be locate inside or outside the CLASS elemnt in the *.hbm.xml file. So I tried to move the sql-query inside the CLASS element, but hibernate throwed exception when executed the sql-query. It could not find the sql-query. This confused me.


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

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.