Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 784375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:42:11+00:00 2026-05-14T20:42:11+00:00

is there any parameter i can set in hibernate so that temporary not allow

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:42:12+00:00Added an answer on May 14, 2026 at 8:42 pm

    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 Session instances created by the SessionFactory. I imagine AOP can be used to proxy the LocalSessionFactoryBean to intercept the created SessionFacotory instances, 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 LocalSessionFactoryBean in custom implementation:

    <bean name="sessionFactory" class="ReadOnlySessionFactoryBean">
       <constructor-arg>
          <bean class="LocalSessionFactoryBean">
           <!-- your existing session factory bean, 
                all your existing hibernate mappings, properties etc. -->
          </bean>
       </constructor-arg>
    </bean>
    

    Then add this class to your codebase. (Even with AOP, you will need to introduce some custom code.)

    public class ReadOnlySessionFactoryBean extends AbstractFactoryBean
    {
        private AbstractSessionFactoryBean sessionFactoryBean;
    
        public ReadOnlySessionFactoryBean(AbstractSessionFactoryBean sessionFactoryBean)
        {
            this.sessionFactoryBean = sessionFactoryBean;
        }
    
        @Override
        public Class getObjectType()
        {
            return sessionFactoryBean.getObjectType();
        }
    
        @Override
        protected Object createInstance() throws Exception
        {
            SessionFactory factory = sessionFactoryBean.getObject();
            return new WrapSessionFactory(factory);
        }
    
        static class WrapSessionFactory implements SessionFactory
        {
            private Sessionfactory delegate;
    
            WrapSessionFactory(SessionFactory delegate)
            {
                this.delegate = delegate;
            }
    
            // delegate most methods to the delegate SessionFactory
    
            // override all the openSession(...) methods
            public Session openSession()
            {
                Session session = delegate.openSession();
                session.setDefaultReadOnly(true);
                return session;
            }
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any parameter that will read whole line from the input? I know
Is there any method that developers can use to check whether particular parameter is
Is there any compiler that has a directive or a parameter to cast integer
Is there any simplified Data Source Control that would allow binding to a local
Is there any way to store the generic parameter type passed in at construction
Is there any way, given a function passed as a parameter, to alter its
Is there any benefit (or conversely, cost) to passing a parameter by const value
Just wanted to check if there is any configuration or parameter to get statistics
Is there any order or pattern that action parameters in an MVC controller follow.
Is there any way how to view all sent parameters if I do not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.