GeeCON Prague 2014*
The moose likes Object Relational Mapping and the fly likes Implementing Hibernate on legacy system Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Implementing Hibernate on legacy system" Watch "Implementing Hibernate on legacy system" New topic
Author

Implementing Hibernate on legacy system

Akshay Bondre
Greenhorn

Joined: Sep 04, 2008
Posts: 13
We are planning to start using Hibernate in our browser based Enterprise Application. We are trying to implement it on a small part of our core structures to see how feasible it would be for us to use on the legacy code and database. Few queries regarding the same..

Intitially we wanted to keep one transaction open for all the read operations and keep on opening and closing other transactions for write operations (Since it needs a commit). But, it seems we cant keep two transactions open on the same session. So, we tried opening a new session for write operations, but again we cannot share the dettached object read from session 1 (which is still open) and save it through session 2.

I could read a obj from session 1, detach it by committing the transaction on session 1 and then reuse the object on session 2 by updating it. But, this would mean lot of sessions/transactions opening and closing.

Any suggestions on the basic design or path way to implement hibernate on the existing code?

Would hibernate 3.3 be of any help?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Creating/opening a Session is extremely lightweight. It is just a bunch of maps and getting a connection from the pool.

Also, don't be afraid of creating transactions too.

Sessions should always be short lived.

What is the rest of your architecture/design? Are you using a transaction manager? Are you in any container?

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Akshay Bondre
Greenhorn

Joined: Sep 04, 2008
Posts: 13
We follow the MVC architecture. Nope, no transaction manager or containers.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Originally posted by akkyy bond:
We follow the MVC architecture. Nope, no transaction manager or containers.


Explain further. MVC in Swing or Web Development. If you are doing web development then you are using a web container, so you do have a container. But if you have o transaction manager and not using a framework to handle transactions, then you are only working with HibernateTransactions, which is fine, and is still very light weight and you should not worry about creating them the Session to Transaction is a one to one relationship and is fine.

Mark
Akshay Bondre
Greenhorn

Joined: Sep 04, 2008
Posts: 13
Oh ok, my bad.

We use JSP's & Servlets as web container. There is no rigid framework followed though.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Originally posted by akkyy bond:
Oh ok, my bad.

We use JSP's & Servlets as web container. There is no rigid framework followed though.


OK, you are using JSP and Servlets as a technology, and probably Tomcat as the Web Container.

With that in mind, you will either choose to use just the Hibernate Transactions, where a Session is one to one with the Transaction. Still not really a problem. Or include a JTA Transaction manager, but you could also do something like use Spring to get transactions demarcated at the service/use case level.

Speaking of which, try to layer your application. Do not put any business logic or Hibernate code directly in a Servlet. Have your Servlet only get any information out of the Request, and create some Service class and call one method on it that will handle all the business logic. That Service class should call a DAO class to do your queries and database CRUD calls.

Mark
Akshay Bondre
Greenhorn

Joined: Sep 04, 2008
Posts: 13
Ok.

My main intention was to fill up some base structures of the product using Hibernate on the login. But, now I realized, I cannot read an object structure through Hibernate and keep it ready to use for the user.

The lazy fetched data isn't available once i close the session. And I cant possible keep one session alive through out the life of a user login so that the object(structure) read can be traversed to access information.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Originally posted by akkyy bond:
Ok.

My main intention was to fill up some base structures of the product using Hibernate on the login. But, now I realized, I cannot read an object structure through Hibernate and keep it ready to use for the user.

The lazy fetched data isn't available once i close the session. And I cant possible keep one session alive through out the life of a user login so that the object(structure) read can be traversed to access information.


Exactly.

Now when you say base structures, do you mean lists for drop down like states. If you mean data like that that all users need and rarely if ever gets changed, then you might want to look into adding the second level cache. like ehcache or JBoss Cache.

If you structure is a place holder of some sort, or data that you retrieve only once when the user logs in and keeps all the data it needs for every use case in it, then you can load up that object with all that data and store the object in the Web Session, not the Hibernate Session.

Mark
Akshay Bondre
Greenhorn

Joined: Sep 04, 2008
Posts: 13
As of now, once the structures are fetched, they are kept in memory for users to use and modify often.

I was successful to design a class structure that comprises of this data. The structure reads the data the way I want it to and also saves it + I get data that I want by simply calling the accessors on those classes. Everything is wonderful, just for this session management thingie.

If I read the object initially and keep it in my Web Session, would I be able to fetch the lazy data? I think I would need a Hibernate session and update that object before I ask something out of it. Or not?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Implementing Hibernate on legacy system