Hibernate and Spring transactions - using private constructors/static factory methods

We have a Hibernate/Spring application that have the following Spring beans:

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" />

When wiring the application together we get the following error when using private constructors in our hibernate entities:

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No visible constructors in class 'ourclass'

The entities are typical domain objects such as an Employee or the like.

When changing the constructor's visibility modifier to package (or public) the application runs fine and the entities gets stored/loaded in the database. How do we/can we use private constructors/static factory methods with Spring/Hibernate transaction management?

We use Hibernate annotations to map the entities/relationships. No bean definitions are declared in the applicationContext.xml for the domain class that is related to the problem. It is a pojo that should have a static factory method and a private constructor.

How can we make Hibernate (org.springframework.spring-orm.hibernate3 classes i guess) make use of the static factory method instead of the constructor? Or possibly make it call a private constructor if necessary?

Using the spring factory-method configuration would make sense but the entities are not mapped as beans in our applicationContext.xml. They are only annotated with the @Entity annotation for Hibernate persistence.

Hope this edit clearifies (rather than mystifies) the question. :)


While I haven't used Spring, I have used Hibernate in a project which has classes which must either be instantiated by factory methods or through multiple argument constructors.

You can do this through an Interceptor, which is a class which listens in to several key hibernate events, such as when an object needs to be instantiated or when an object is loaded.

To make Hibernate use your own means of instantiating the object, do something like this:

public class MyInterceptor extends EmptyInterceptor {

    public Object instantiate(String entityName, EntityMode entityMode, Serializable id) {
             return Foo.create();
        return null;

I'm a little suprised that you are having issues with Hibernate not instantiating objects with a non visible constructor, considering that it can be worked around with reflection and I haven't had that issue in my project (non of the classes actually have visible constructors). It may be something with Spring. Check which version of hibernate you are using as well.

Need Your Help

iCarousel view not showing up in ipad

ios ipad carousel

I am making an ios application on ipad and i am putting a iCarousel view in my application by the help of this script :

Getting PHP array to JavaScript through Json

jquery ajax arrays json

Hey, I've got a PHP file that that echo's an array that has been encoded with json_encode. This file is used by the jQuery ajax function to retrieve the array. I am unable to figure out how to use ...