-3

this is exception i am getting while executing spring application.can anyone help me to resolve this problem.

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
                    log4j:WARN Please initialize the log4j system properly.
                    Hibernate: drop table if exists postgres.student007 cascade
                    Hibernate: create table postgres.student007 (id int4 not null,address varchar(255), email varchar(255), name varchar(255), primary key (id) Exception in thread "main" 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.
                    at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
                    at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:620)
                    at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:617)
                    at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340)
                    at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)
                    at org.springframework.orm.hibernate4.HibernateTemplate.save(HibernateTemplate.java:617)
                    at dao.StudentDaoImplHT.save(StudentDaoImplHT.java:22)
                    at test.Client.main(Client.java:21)

below is my configuration config.xml file.can anyone help me to resolve this problem.

      <beans>
        <bean id="bds" class="org.apache.commons.dbcp.BasicDataSource">
           <property name="driverClassName" value="org.postgresql.Driver"/>
           <property name="url" value="jdbc:postgresql://localhost:5432/postgres"/>
           <property name="username" value="postgres"/>
           <property name="password" value="postgres"/>
           <property name="maxActive" value="15"/>
           <property name="maxIdle" value="5"/>
           <property name="maxWait" value="5000"/>
        </bean>

        <!--  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
         -->
           <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
           <property name="dataSource" ref="bds"/>
           <property name="hibernateProperties">
             <props>
               <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
               <prop key="hibernate.hbm2ddl.auto">create</prop>
               <prop key="hibernate.show_sql">true</prop>    
             </props>
           </property>
           <property name="annotatedClasses">
             <list>
               <value>model.Student</value>
             </list>
           </property>
         </bean>
         <bean id="ht" class="org.springframework.orm.hibernate4.HibernateTemplate">
         <property name="sessionFactory" ref="sessionFactory"/> 
         </bean>
        <bean id="dao" class="dao.StudentDaoImplHT">
        <property name="ht" ref="ht"/>
        </bean>
        </beans>
Sunil Kumar
  • 93
  • 1
  • 1
  • 10

1 Answers1

1

It's the problem of spring transaction. You need to config transaction

    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

Then enable the annotation configure for transaction

<tx:annotation-driven />

At last, add the transaction configure in your method.

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)

You problem is that you didn't use the transaction but do writing things into database. It's not allowed, you should create a transaction and make the readOnly = false. Then you can create table.

You can use jdbcTemplate.execute(sql). to create table.

H.Hao
  • 93
  • 2
  • 11