Bean Property configurationClass Is Not Writable [Solved]

Spring throws “Bean property ‘configurationClass’ is not writable” when XML written for Hibernate 3 sets configurationClass on a newer LocalSessionFactoryBean. See the real error on Spring 7, the XML fix, and the Spring Boot 4 setup that needs no SessionFactory bean.

Spring throws “Bean property ‘configurationClass’ is not writable or has an invalid setter method” when an XML bean definition sets configurationClass on a LocalSessionFactoryBean that has no such property. Only the old org.springframework.orm.hibernate3.LocalSessionFactoryBean (Spring 4.3 and older, Hibernate 3) had it. Every later version of the class (orm.hibernate4, orm.hibernate5 and Spring 7’s orm.jpa.hibernate) fails on that line, so the fix is to delete the configurationClass property.

We meet the error when an old Spring and Hibernate 3 project moves to a newer Spring version but keeps its XML bean definitions. For example, a team upgrades a cinema booking app from Spring 3 to Spring 7, and the app stops at startup before it serves a single page.

The following example shows the error that Spring 7.0.9 with Hibernate 7.4 prints for such an XML file.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [legacy/session-factory-broken.xml]: Invalid property 'configurationClass' of bean class [org.springframework.orm.jpa.hibernate.LocalSessionFactoryBean]: Bean property 'configurationClass' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'configurationClass' of bean class [org.springframework.orm.jpa.hibernate.LocalSessionFactoryBean]: ...

Notice the words “Invalid property ‘configurationClass'” in the message. Spring 6.2 prints the same text with orm.hibernate5 in the class name, and Spring 4.3 with orm.hibernate4 adds “Error setting property values; nested exception is” in front of it. The error comes from one line in the bean definition.

<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>

Next, we see why Spring cannot set the property and how to fix the XML. After that, we let Spring Boot create the factory, so the XML bean is gone altogether.

1. Why Spring Cannot Set configurationClass

A bean is an object that Spring creates and configures for us. For every <property name=”x”> in an XML bean definition, Spring calls the matching setter, setX(), on the bean class. When the class has no such setter, Spring throws NotWritablePropertyException and the bean is never created. The sessionFactory bean then fails, and so does every DAO that needs it.

Left: the XML bean definition of sessionFactory with the properties dataSource, configurationClass and annotatedClasses. Right: the setters of org.springframework.orm.jpa.hibernate.LocalSessionFactoryBean in Spring 7.0.9: setDataSource and setAnnotatedClasses are found, setConfigurationClass does not exist. The missing setter leads to NotWritablePropertyException wrapped in BeanCreationException, and the context does not start
Each <property> needs a setter. configurationClass has none in the current LocalSessionFactoryBean, so the whole context stops.

The property did exist once. In Hibernate 3, a separate class, AnnotationConfiguration, read the annotations, and Spring’s orm.hibernate3 factory bean let us choose it with configurationClass. The spring-orm jars from Maven Central show which versions have the setter.

Spring packageSpring versionssetConfigurationClass()Entities registered with
orm.hibernate3up to 4.3Yeshbm.xml files, or annotatedClasses on AnnotationSessionFactoryBean
orm.hibernate43.1 to 4.3NopackagesToScan, annotatedClasses
orm.hibernate54.2 to 6.2NopackagesToScan, annotatedClasses
orm.jpa.hibernate7.0 and laterNopackagesToScan, annotatedClasses

Hibernate itself dropped AnnotationConfiguration as well. Hibernate 3.6 deprecated the class because the plain Configuration class reads annotations, and AnnotationConfiguration is missing from Hibernate 5.0 onward. So an XML file copied from a Spring 3 / Hibernate 3 tutorial fails as soon as the project moves to a newer factory bean.

Four columns: orm.hibernate3 (Spring 4.3 and older, Hibernate 3, LocalSessionFactoryBean or AnnotationSessionFactoryBean, configurationClass yes); orm.hibernate4 and 5 (Spring 3.1 to 6.2, Hibernate 4 to 6, packagesToScan and annotatedClasses, configurationClass no); orm.jpa.hibernate (Spring 7, Hibernate 7, same properties, configurationClass no); Spring Boot with JPA (no SessionFactory bean, entityManagerFactory is a SessionFactory, no XML)
configurationClass exists only in the first column, so every later LocalSessionFactoryBean rejects it, and Spring Boot needs no factory bean at all.

2. How to Fix It

There are two ways out. When an old project keeps its XML configuration, we delete the property and register the entities, and when a project can move to Spring Boot, we drop the SessionFactory bean altogether.

2.1. Remove configurationClass From the XML

We delete the configurationClass line and tell the factory bean where the entity classes are. The following example is a cinema with one Showtime entity that has a movie title, a screen number and a start time.

<bean id="sessionFactory" class="org.springframework.orm.jpa.hibernate.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
  <property name="annotatedClasses">
    <list>
      <value>com.howtodoinjava.hibernate.springconfig.Showtime</value>
    </list>
  </property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.jpa.hibernate.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="packagesToScan" value="com.howtodoinjava.hibernate.springconfig"/>
  <property name="hibernateProperties">
    <props>
      <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
      <prop key="hibernate.show_sql">true</prop>
    </props>
  </property>
</bean>

The property packagesToScan finds every @Entity class in the package. Keeping annotatedClasses without configurationClass works too, but then we must add every new entity to the list. With either property, the context starts, and the SessionFactory bean saves and reads showtimes.

SessionFactory sessionFactory = context.getBean(SessionFactory.class);

sessionFactory.inTransaction(session -> {
  session.persist(new Showtime("Dune", 1, LocalDateTime.of(2026, 10, 9, 18, 30)));
  session.persist(new Showtime("Dune", 2, LocalDateTime.of(2026, 10, 9, 21, 0)));
});                                   // insert into Showtime (movieTitle,screen,startsAt,id) values (?,?,?,?)

List<Showtime> shows = sessionFactory.fromTransaction(session ->
    session.createSelectionQuery("from Showtime order by startsAt", Showtime.class).getResultList());
// [Dune (screen 1, 2026-10-09T18:30), Dune (screen 2, 2026-10-09T21:00)]

We also check the package in the class attribute. Spring 7 removed orm.hibernate5, so an XML file written for Spring 5 or 6 fails one step earlier, before any property is set.

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate5.LocalSessionFactoryBean] for bean with name 'sessionFactory' defined in class path resource [legacy/session-factory-hibernate5.xml]
Spring versionClass name to use
3.1 to 4.3org.springframework.orm.hibernate4.LocalSessionFactoryBean
4.2 to 6.2org.springframework.orm.hibernate5.LocalSessionFactoryBean
7.0 and laterorg.springframework.orm.jpa.hibernate.LocalSessionFactoryBean

2.2. Let Spring Boot Create the SessionFactory

In a Spring Boot 4.1.1 application, we declare no SessionFactory bean, no XML and no configurationClass. The JPA starter brings Hibernate 7 and Spring Data JPA.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
</dependency>
spring.datasource.url=jdbc:h2:mem:showtimes;DB_CLOSE_DELAY=-1
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true

Spring Boot finds the entities in the package of the @SpringBootApplication class. It creates the data source and the entityManagerFactory bean, plus a JPA transaction manager.

Spring Boot creates dataSource, entityManagerFactory (an EntityManagerFactory that is also a SessionFactory) and a JPA transactionManager from application.properties and the entity classes. Our code uses a JpaRepository, a native Session via em.unwrap(Session.class), or the SessionFactory itself. LocalSessionFactoryBean, configurationClass, HibernateTransactionManager and hibernate.cfg.xml are no longer needed; sessionFactory.getCurrentSession() throws No CurrentSessionContext configured by default
One auto-configured entityManagerFactory serves Spring Data repositories, JPA code and native Hibernate code.

A repository interface replaces the old DAO that wrapped the SessionFactory.

public interface ShowtimeRepository extends JpaRepository<Showtime, Long> {

  List<Showtime> findByScreenOrderByStartsAt(int screen);
}
repository.save(new Showtime("Up", 3, LocalDateTime.of(2026, 10, 10, 14, 0)));
repository.save(new Showtime("Coco", 3, LocalDateTime.of(2026, 10, 10, 11, 0)));

List<Showtime> screenThree = repository.findByScreenOrderByStartsAt(3);
// select s1_0.id,s1_0.movie_title,s1_0.screen,s1_0.starts_at from showtime s1_0 where s1_0.screen=? order by s1_0.starts_at
// [Coco (screen 3, 2026-10-10T11:00), Up (screen 3, 2026-10-10T14:00)]

The default naming strategy of Spring Boot writes movie_title and starts_at, whereas the XML setup from section 2.1 keeps the field names (movieTitle, startsAt).

2.3. Getting the SessionFactory in Spring Boot

Code that still needs the native Hibernate API does not need its own factory bean. The entityManagerFactory bean that Spring Boot creates is also a SessionFactory.

boolean isFactory = emf instanceof SessionFactory;                   // true
String[] names = context.getBeanNamesForType(SessionFactory.class);  // [entityManagerFactory]

SessionFactory sessionFactory = emf.unwrap(SessionFactory.class);
Long count = sessionFactory.fromTransaction(session ->
    session.createSelectionQuery("select count(*) from Showtime", Long.class).getSingleResult());   // 2

We can also inject SessionFactory into a constructor. For DAO methods that run inside a Spring transaction, we take the Session from the transactional EntityManager.

@Transactional
public void save(Showtime showtime) {
  entityManager.unwrap(Session.class).persist(showtime);
}
// insert into showtime (movie_title,screen,starts_at,id) values (?,?,?,?)

The complete project on GitHub reproduces both errors and runs both XML fixes and the Spring Boot setup, with 12 JUnit tests that check every result (mvn -q compile exec:java, mvn test).

3. configurationClass Error FAQs

3.1. What Replaced AnnotationConfiguration?

The plain org.hibernate.cfg.Configuration class replaced it, because it reads annotations itself since Hibernate 3.6. Hibernate 7 has no AnnotationConfiguration at all, so Class.forName() throws ClassNotFoundException, and a setup without Spring creates a Configuration itself.

SessionFactory sessionFactory = new Configuration()
    .addAnnotatedClass(Showtime.class)
    .setProperty("hibernate.connection.url", "jdbc:h2:mem:plain;DB_CLOSE_DELAY=-1")
    .setProperty("hibernate.connection.username", "sa")
    .setProperty("hibernate.hbm2ddl.auto", "create-drop")
    .buildSessionFactory();

For new code without Spring, Hibernate 7 also offers HibernatePersistenceConfiguration, which builds a JPA EntityManagerFactory without a persistence.xml file.

3.2. Why Does getCurrentSession() Fail in Spring Boot?

The call getCurrentSession() fails because the JPA setup of Spring Boot configures no current session context, so old DAOs that call sessionFactory.getCurrentSession() get a HibernateException. A typical case is a DAO copied from the XML project that still calls getCurrentSession() in every method.

org.hibernate.HibernateException: No CurrentSessionContext configured

We replace getCurrentSession() with entityManager.unwrap(Session.class) inside a @Transactional method, as in the ShowtimeDao from section 2.3. The session then joins the same transaction as the repositories.

3.3. Do Other Hibernate 3 Settings Break the Same Way?

Yes. An upgrade from Hibernate 3 often leaves more leftovers next to configurationClass, and each one fails with its own message.

  • A cache setting that names a CacheProvider class fails, because Hibernate 4 removed the org.hibernate.cache.CacheProvider interface.
  • A library compiled against SessionFactoryImplementor in org.hibernate.engine fails, because Hibernate 4 moved it to org.hibernate.engine.spi.

Both changes are visible when we compare the Hibernate 3.6.10 and 4.3.11 jars. So we search the whole XML file and the dependency list for Hibernate 3 class names, not only the sessionFactory bean.

4. Conclusion

“Bean property ‘configurationClass’ is not writable” means the XML was written for Spring’s orm.hibernate3 factory bean, and the project uses a newer LocalSessionFactoryBean that has no such setter. We delete the property, register the entities with packagesToScan or annotatedClasses, and use the class name that matches the Spring version. In a Spring Boot application, the JPA starter creates the SessionFactory for us, and unwrap() gives native Hibernate code what it needs.

5. References

Happy Learning !!

Source Code on Github

Leave a Comment

  1. I am new to Hibernate, I have a Problem,

    How to resolve the Exception “com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection”?

    my application uses Hibernate. I am working on an ITS(Intelligent Transport System) So here every 10 seconds I have track the position(latitude, longitude) of bus. I call a ajax method every 10 sec which makes request to servlet. My application works fine for some time period, after that it shows error as ,

    org.hibernate.exception.JDBCConnectionException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
    at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
    at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
    at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
    at com.aurodisplay.its.DAO.FetchVehicleDetails.getAllVehicleDetails(FetchVehicleDetails.java:25)
    at com.aurodisplay.its.ajax.RealTimeMoniotring.doPost(RealTimeMoniotring.java:81)
    at com.aurodisplay.its.ajax.RealTimeMoniotring.doGet(RealTimeMoniotring.java:42)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

    and

    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: “Too many connections”
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1013)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1136)

    I am not getting how to resolve it. I searched many resource and found that it is about managing connection pool in hibernate configuration file.

    So here is my Hiberenate configuration file.


    com.mysql.jdbc.Driver
    jdbc:mysql://localhost:3306/bus_serverdb
    root
    root


    1


    org.hibernate.dialect.MySQLDialect


    org.hibernate.cache.NoCacheProvider


    true


    <!– create –>

    Can any one tell me what are the changes I have to do and why?

    • I am not sure why this exception is happening to your application but “Too many connections” usually suggest two things:
      1) No. of connections in pool needs to be increased to cater application’s genuine needs
      2) Your code is faulty and does not close the connections some of the times. I will suggest you to close the connections in finally blocks if you are not doing it already; AND debug the application to see if some exceptions are happening before actually closing the connections.

      • Here is my sample code,

        Session sessio = HibernateSession.getHibernateSession();
        Transaction transaction = sessio.beginTransaction();
        try {
        //coding stuff.

        }
        catch (Exception e) {
        if (transaction!=null) transaction.rollback();
        e.printStackTrace();
        } finally {
        sessio.close();
        }

        Here everything works fine only for some particular time period.

  2. Is it possible to change LocalSessionFactoryBean’s dataSource dynamically?

    I have defined it correclty in spring’s configuration file, and there is tied TransactionManager, TransactionProxyFactoryBean, and some Dao’s to it, and i need to copy rows from one datasource to another.

    The interesting point is, that we define that destination datasource runtime, and so i cannon’t define it to configuration file.

    I tried get LocalSessionFactoryBean from ApplicationContext by getBean(String) method, but it returned SessionFactory, where i cannon’t change datasource..

  3. As per my requirement i need to create Excel Report. In that report some cell should have formula. I tried to set formula for particular cell like below i got org.apache.poi.ss.formula.FormulaParser.parse Exception in line no 3. Kindly help me on this

    1 .String ref = (char)(‘E’ ) + “10:” + (char)(‘I’) + “10”;
    2. dataCell.setCellType(Cell.CELL_TYPE_FORMULA);
    3 .dataCell.setCellFormula(“SUM(” + ref + “)”);

    Full Error Log:

    javax.el.ELException: java.lang.NoSuchMethodError: org.apache.poi.ss.formula.FormulaParser.parse(Ljava/lang/String;Lorg/apache/poi/ss/formula/FormulaParsingWorkbook;II)[Lorg/apache/poi/ss/formula/ptg/Ptg;
    at com.sun.el.parser.AstValue.invoke(Unknown Source)
    at com.sun.el.MethodExpressionImpl.invoke(Unknown Source)
    at org.apache.myfaces.trinidadinternal.taglib.listener.FileDownloadActionListener.processAction(FileDownloadActionListener.java:121)
    at oracle.adfinternal.view.faces.event.rich.FileDownloadActionListener.processAction(FileDownloadActionListener.java:77)
    ……………
    Caused by: java.lang.NoSuchMethodError: org.apache.poi.ss.formula.FormulaParser.parse(Ljava/lang/String;Lorg/apache/poi/ss/formula/FormulaParsingWorkbook;II)[Lorg/apache/poi/ss/formula/ptg/Ptg;
    at org.apache.poi.xssf.usermodel.XSSFCell.setFormula(XSSFCell.java:439)
    at org.apache.poi.xssf.usermodel.XSSFCell.setCellFormula(XSSFCell.java:419)
    at com.dev.reports.ExcelReport.java.populateData(ExcelReport.java:178)
    at com.dev.reports.ExcelReport.java.populateDataToWorkBook(ExcelReport.java:132)

    • Really hard to solve without context. General way is:

      String strFormula= "SUM(A1:A10)";
      cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
      cell.setCellFormula(strFormula);

      Now why are you using “char” with E and i. What are the values you are trying to sum?

Comments are closed.

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.