is there any parameter i can set in hibernate so that temporary not allow persist anything but only allow read only (temporary) ? can i set in applicationcontext.xml?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Hibernate has a method
Session.setReadOnly(Object persited, boolean readOnly)that allows you to mark a persisted object as read-only.Hibernate 3.5 has also
Session.setDefaultReadOnly(boolean)that can be used to set all objects retrieved by a session to read only.The challenge then is to find a way of setting this property on the
Sessioninstances created by theSessionFactory. I imagine AOP can be used to proxy the LocalSessionFactoryBean to intercept the createdSessionFacotoryinstances, delegating most methods to the original instance, but intercepting theopenSession(...)methods. But I’m not that familiar with spring AOP, and IMO it can quickly become quite hard to understand. Here’s a direct approach:First, wrap your
LocalSessionFactoryBeanin custom implementation:Then add this class to your codebase. (Even with AOP, you will need to introduce some custom code.)
With just this change, the remainder of your code can run unmodified, and each time will get a read-only SessionFactory. If you wanted to be absolutely sure no writes are being done to the database, you could override the methods that write to the db, e.g.
saveOrUpdate,update, although I’m not sure that is really necessary.