Search code examples
hibernateconfigurationsessionfactory

hibernate SessionFactory ob


Consider Hibernate client code

Configuration cfg = new Configuration();
cfg.configure();

at this point the default constructor of persistence class will not call. That means no instance will create for persistence class

but after creating SessionFactory object i.e

Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();

default construcort will call 3 times exactly so the question is why exactly 3 persistence class objects will created after creating SessionFactory object.


Solution

  • It is an interesting notice.

    Hibernate gets all information about the mapping by loading and checking corresponding Class<?> objects. But, sometimes, it needs an information that impossible to get from a Class<?> object, for an example, a result of calling some method of a persistent. In such situations Hibernate creates a temporary object of a persistent and call a method of it.

    You can set a breakpoint on the default constructor and check all situations in which Hibernate instantiate persistent objects.

    I try to test this behaviour with Hibernate 4 and Hibernate 5. In both cases a default constructor is called only once in this method, in the line

    final Serializable defaultValue = (Serializable) identifierGetter.get( instantiate( constructor ) );

        /**
         * Return an IdentifierValue for the specified unsaved-value. If none is specified, 
         * guess the unsaved value by instantiating a test instance of the class and
         * reading it's id property, or if that is not possible, using the java default
         * value for the type
         *
         * @param unsavedValue The mapping defined unsaved value
         * @param identifierGetter The getter for the entity identifier attribute
         * @param identifierType The mapping type for the identifier
         * @param constructor The constructor for the entity
         *
         * @return The appropriate IdentifierValue
         */
        public static IdentifierValue getUnsavedIdentifierValue(
                String unsavedValue,
                Getter identifierGetter,
                Type identifierType,
                Constructor constructor) {
            if ( unsavedValue == null ) {
                if ( identifierGetter != null && constructor != null ) {
                    // use the id value of a newly instantiated instance as the unsaved-value
                    final Serializable defaultValue = (Serializable) identifierGetter.get( instantiate( constructor ) );
                    return new IdentifierValue( defaultValue );
                }
                else if ( identifierGetter != null && (identifierType instanceof PrimitiveType) ) {
                    final Serializable defaultValue = ( (PrimitiveType) identifierType ).getDefaultValue();
                    return new IdentifierValue( defaultValue );
                }
                else {
                    return IdentifierValue.NULL;
                }
            }
    
            ...
        }