• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alternatives to Open Session in View pattern?

 
joe black
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am curious as to what alternatives others use to the Open Session in View pattern?

http://www.hibernate.org/43.html

I want my DAO layer to be completely seperate from other layers. I was thinking of having a 1 to 1 mapping between my classes and the db. i.e all fields of the class would map to all fields of the table. And then at a higher level, I can just grab what I need. If I need all students in a course, do something like

Course course = CourseDAO.findCourseByXXX();
List students = StudentDAO.findStudentsByCourseId( course.getId() );

This way I can avoid mapping collections, and dont need to use lazy loading, etc... The hibernate mappings would be really simple as well. And then use a second level cache as well. Does anybody use an approach like the above?

What are some other alternatives?
[ September 15, 2006: Message edited by: peter black ]
 
Jonathan Huang
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The alternatives are what you can dream up.

Session per transaction
Session per app
Session per HTTPSession

Session for every 2 transactions....

etc. . .

It all depends on how many users may potentially be using the system, how well you want to be able to grow your system, etc, that will ultimately decide which way to go with your session.

The reason to use the session in view pattern is so you can lazy load objects in the view. It is easy to write a filter that opens and closes the thread bound session so you don't have to take care of it programatically.

Also, the Spring framework comes with some handy features such as transaction AOP point cuts and a OpenSessionInViewFilter already made for you. It removes all the boiler plate code for you, and you can focus on your business logic.

As to not mapping collections . . . Sounds interesting. Let me know how that goes. And if you don't have any collections you could use the session per transaction strategy. It works very well for simple schemas.

Look at the CaveatEmptor example hibernate provides. You can also check out a plugin for Eclipse called HibernateSync. I wouldn't recommend using it, but just to look at the code it generates. Gives you insight on other posibilities.

good luck
 
Anthony Karta
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jonathan Huang:

Also, the Spring framework comes with some handy features such as transaction AOP point cuts and a OpenSessionInViewFilter already made for you. It removes all the boiler plate code for you, and you can focus on your business logic.


I know this is Hibernate forum and old post,

I have been reading about lazy loading for few days, and I confuse and wonder...

Does Spring OpenSessionInViewFilter and Interception solve the lazy loading exception in front-end layer gracefully?

any good example?

at the moment, I feel Hibernate is half good, should I use it? or just use Spring JdbcDAOSupport and its JdbcTemplate?

TIA
 
Avinash Ramana
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The OSIV pattern can be easily implemented by using OpenSessionInView{Filter|Interceptor}. By using the OSIV pattern, you essentially couple the service layer to the view layer. This may or may not be a problem in the future. I've used OSIV for a number of applications without problems.

If you don't want to use OSIV, some people on the Spring board have "iterated" through a collection to initialize them. This method works well in addition to OSIV. I have not heard of someone trying to use one-to-one in order to get around the lazy loading issue. I always look at lazy loading as a good thing!

If you want much more "code" type examples, you should search the Spring forums.

-Avinash R.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic