• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comment on this hibernate util class

 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have around 20 Dao classes, each containing 4-6methods. So, i went for a separate Utility class. Here is the code :



Dao class



Give your valuable suggestions and improvements needed in this code
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's been a while, more into JPA these days, but have you considered the transactional nature of your solution?

WP
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some issues with it.

1. The biggest issue is your private static final Session. Sessions are not thread safe. SessionFactories are.
2. config.buildSessionFactory() is deprecated as is probably evidenced by the strike through you most likely see in your IDE.
3. What is it are you hoping to accomplish with your exception handling? You might want to throw an exception in your create session and see the message you are actually throwing out on that exception.

Try something more along these lines

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider if you want to use one hibernate session through one user request(using same session across multiple DAOs). As far as I know it is not a wise decision to open and close session for every DB transaction.

Is it a web application? Then you might want to use same hibernate session in a single user session. If so where do you want to place your session?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks William P O'Sullivan, Bill Gorder and Divya Janyavula for your replies.
Its a Windows application, created using swing and hibernate.

Can anyone explain the internal proceedings if i go for getCurrentSession? In my application, i haven't used cache. The whole application basically works on CRUD.
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Discussed here:
https://coderanch.com/t/218463/ORM/databases/Hibernate-sessions-transactions
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic