2008年12月9日 | 分类: java | 标签: , ,

I have a @ManyToMany property. When I do an update, hibernate issues a delete statement to delete all the related records in the cross reference table for the collection and then issues an insert statements to re-insert all the records back into the cross reference table again. The update performs correctly, but the id which is the primary key is always got changed by the trigger I set for the cross reference table, even I don’t do any update to any of the fields.

Hibernate: delete from A_B where A_ID=?
Hibernate: insert into A_B (A_ID, B_ID) values (?, ?)

阅读全文…

2008年12月6日 | 分类: java | 标签:

Have you ever encountered the following error message?

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'add' must be of type [org.springframework.web.servlet.View], but was actually of type [your.package.Controller]
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:309)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:174)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:227)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:174)
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:887)
org.springframework.web.servlet.view.ResourceBundleViewResolver.loadView(ResourceBundleViewResolver.java:188)
org.springframework.web.servlet.view.AbstractCachingViewResolver.createView(AbstractCachingViewResolver.java:159)
org.springframework.web.servlet.view.AbstractCachingViewResolver.resolveViewName(AbstractCachingViewResolver.java:78)
org.springframework.web.servlet.DispatcherServlet.resolveViewName(DispatcherServlet.java:1190)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1139)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:808)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

I had that when I tried to set up two view handlers in my Spring configuration file. It was working when I only had SimpleUrlHandlerMapping, until I added ResourceBundleViewResolver. Here was my problem configurations.
阅读全文…

2008年11月22日 | 分类: java | 标签: , , ,

配置Hibernate和Spring的Open Session in View模式和事务

At work, I took on a job to manage a new web application with Spring and Hibernate. I really have been through a tough time to learn and to work with Spring and Hibernate frameworks. I don’t have a lot of experience with them, and also no one around me has, which makes the development process even harder, especially when trying to use OSIV (Open Session in View) in Spring Web MVC. Here are the notes.

At the database level, I have many eagerly-load ManyToMany and ManyToOne (OneToMany) relationships. It made a single page loading very slow. Every time a page tried to display something, it would fetch everything related based on those relationships defined in persistent objects by annotations. The first thing came to my mind was to use lazy initialization and hoped that collections could be fetched lazily. Here came the first error when I tried to load a collection in a persistent object:

Failed to lazily initialize a collection - no session or session was closed
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed

阅读全文…