Shaaban Ebrahim
Shaaban Ebrahim

Reputation: 10422

Why i can not change flushmode in hibernate

org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

I tried to change it in code and in xml to another flushmode but it is still Auto.

hibernatetemplate.getSessionFactory().openSession().setFlushMode(FlushMode.COMMIT);

and <prop key="org.hibernate.FlushMode">COMMIT</prop>

Upvotes: 1

Views: 1392

Answers (3)

Shaaban Ebrahim
Shaaban Ebrahim

Reputation: 10422

actually i solved it

final Person object = new Person(id, name, password);
    hibernateTemplate.execute(new HibernateCallback<Person>() {

        public Person doInHibernate(Session session)
                throws HibernateException {

            session.save(object);
            session.flush();
            return object;
        }
    });

Upvotes: 0

Anwar Mohamed
Anwar Mohamed

Reputation: 11

you can make hibernateTemplate object and override excute method

Upvotes: 1

Maciej Kowalski
Maciej Kowalski

Reputation: 26522

Most likely your operations, some of them being persiting or updating entities, on the session are not within a transactional context.

Try to enclose them within:

Session session = hibernatetemplate.getSessionFactory().openSession();
Transaction tx = session.beginTrasaction();

...

tx.commit();
session.close();

Now, when you begin the transaction, the flush mode should be implicitly set to COMMIT/AUTO.

Upvotes: 1

Related Questions