aspose file tools*
The moose likes Other Application Frameworks and the fly likes How to save and delete the List<Object> by HibernateTemplate?.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "How to save and delete the List<Object> by HibernateTemplate?.." Watch "How to save and delete the List<Object> by HibernateTemplate?.." New topic
Author

How to save and delete the List<Object> by HibernateTemplate?..

Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Hi Friends..

i have a little question about how to save and delete the List<Object> by HibernateTemplate?..

example i have List<Question> which named questionsCollection..

in my case i save by HibernateTemplate like this :


and i delete by HibernateTemplate like this :


i can save all the List<Question> by looping and save each Question..
getHibernateTemplate().save(question);

which one is better for implementation?..
Thanks before ..

Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Well, in my opinion is you want to have the delete all happen in the same transaction and same commit, so that it is all batched. I would not want to see code that loops through the collection and saves each one individually, I think it would slow things down. And I always go as easy and as less code as necessary.

My bigger opinion is that I never use the HibernateTemplate because with the latest Hibernate's I get absolutely no benefit from the Spring HibernateTemplate. With that in mind, know that I used to work at SpringSource, and I highly recommend using Spring and their classes, but this is one that no longer is needed.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Mark Spritzler wrote:
My bigger opinion is that I never use the HibernateTemplate because with the latest Hibernate's I get absolutely no benefit from the Spring HibernateTemplate. With that in mind, know that I used to work at SpringSource, and I highly recommend using Spring and their classes, but this is one that no longer is needed.


Thanks Mark for your post reply first..

is it any another way to integrating Spring and Hibernate with not use HibernateTemplate?
if we develop an application based on Spring, what technology is good for The Persistence Layer?..
is it just use a Persistence Class which provided by Spring?..

iam sorry before, iam still newbie..
in my application i just use Spring for IoC and DAO layer..
maybe i think i will learn more about SpringMVC, and IoC..

Thanks in advance

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Leonardo Carreira wrote:
Mark Spritzler wrote:
My bigger opinion is that I never use the HibernateTemplate because with the latest Hibernate's I get absolutely no benefit from the Spring HibernateTemplate. With that in mind, know that I used to work at SpringSource, and I highly recommend using Spring and their classes, but this is one that no longer is needed.


Thanks Mark for your post reply first..

is it any another way to integrating Spring and Hibernate with not use HibernateTemplate?
if we develop an application based on Spring, what technology is good for The Persistence Layer?..
is it just use a Persistence Class which provided by Spring?..

iam sorry before, iam still newbie..
in my application i just use Spring for IoC and DAO layer..
maybe i think i will learn more about SpringMVC, and IoC..

Thanks in advance



Instead of using the HibernateTemplate, you can inject a SessionFactory into your DAO and in your code you just call sessionFactory.getCurrentSession() and then do your work. In the configuration you should already have a SessionFactory configured and a DataSource. The only thing I probably would add is the Hibernate to Spring Exception tree translation, but not completely necessary.

Mark
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Mark, if you haven't used HibernateTemplate, how did you make data access code thread-safe?


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Kengkaj Sathianpantarit wrote:Mark, if you haven't used HibernateTemplate, how did you make data access code thread-safe?


HibernateTemplate is not about thread-safe. Whatever issues you might ever have is about transaction isolation. And Springs Transaction manager class can take a SessionFactory.

So no, that is not an issue at all that is even in consideration for data access code.

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Lcuky me, there is another thread that already has the configuration and stuff from the docs.

http://www.coderanch.com/t/60666/Application-Frameworks/define-Hibernate-SessionFactory-with-Spring

Mark
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
As I know, HibernateTemplate is thread-safe.
http://www.javalobby.org/articles/thread-safe/index.jsp
http://jira.springframework.org/browse/SPR-2419
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
From Spring and Hibernate3 documentation:

1. HibernateTemplate
NOTE: As of Hibernate 3.0.1, transactional Hibernate access code can also be coded in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate3 style of coding data access objects instead, based on SessionFactory.getCurrentSession().

I guess that means if use Hibernate API directly we need to use SessionFactory.getCurrentSession().

2. Session (Hibernate)
It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.
I guess SessionFactory.getCurrentSession() handles thread-safe.

3. SessionFactory (Hibernate)
Implementors must be threadsafe.

getCurrentSession()
throws HibernateException

Obtains the current session. The definition of what exactly "current" means controlled by the CurrentSessionContext impl configured for use.

4. CurrentSessionContext (Hibernate)
Implementations should adhere to the following:

* contain a constructor accepting a single argument of type SessionFactoryImplementor
* should be thread safe
* should be fully serializable

I guess using Hibernate 3 API achieves thread-safety via 2)-4).

What is your opinion, Mark?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Actually, in all the version of Hibernate that I have used I have never had to worry about thread safety, and even if it were, the HibernateTemplate was not created to solve any thread safety, it was to save you code so you didn't have to write the old style hibernate code where you started the transaction and you had to surround in a try-catch and commit or rollback etc. Just like the JDBCTemplate saves you from the excessive code you had to write when doing straight JDBC code.

Mark
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Mark Spritzler wrote:the HibernateTemplate was not created to solve any thread safety

HibernateTemplate solves some thread-safety issue because it's thread-safe.

Anyway, I misunderstood something. HibernateTemplate does help some part, but we also need HibernateTransactionManager to solve thread-safety issue.

Binds a Hibernate Session from the specified factory to the thread, potentially allowing for one thread-bound Session per factory. SessionFactoryUtils and HibernateTemplate are aware of thread-bound Sessions and participate in such transactions automatically. Using either of those or going through SessionFactory.getCurrentSession() is required for Hibernate access code that needs to support this transaction handling mechanism.

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/HibernateTransactionManager.html

So using SessionFactory.getCurrentSession() and HibernateTransactionManager also does make code thread-safe.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to save and delete the List<Object> by HibernateTemplate?..