Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark, if you haven't used HibernateTemplate, how did you make data access code thread-safe?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic