[hibernate-dev] Building a SessionFactory

Steve Ebersole steve at hibernate.org
Wed Apr 20 23:43:26 EDT 2011


I think this new API for creating a SessionFactory is starting to firm 
up, so I wanted to put out another call for feedback before we get too 
close to Alpha3.  So at a high level there are 2 pieces of information 
needed to build the SessionFactory: the ServiceRegistry and the Metadata.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The ServiceRegistry is built through a ServiceRegistryBuilder (this is a 
slight recent change)

Map config = ...;
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder( config )
         ...
         .buildServiceRegistry();

The "..." allows you to add service instances and service initiators. 
Currently any (map of) configuration values is passed in the 
constructor.  I can be convinced to allow adding/setting of config 
values as well, if everyone has a preference for that approach:

Map config = ...;
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
         .setConfigurationData( config )
         .setOption( Environment.SHOW_SQL,
true )
         ...
         .buildServiceRegistry();

Not sure the best method names there, to be honest which is why I opted 
for passing them to ctor.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Metadata is built through a MetadataSources which represents the sources 
of metadata information.

MetadataSources metadataSources = new MetadataSources( serviceRegistry )
         .addResource( "some.hbm.xml" )
         .addAnnotatedClass( SomeEntity.class );
Metadata metadata = metadataSources.buildMetadata();



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The Metadata is then used to obtain a SessionFactory.

SessionFactory sf = metadata.buildSessionFactory();


Metadata represents the "configuration time" mapping information.  As of 
now we will allow users to manipulate and otherwise access this 
information, hence the seeming "intermediate step".  These all work with 
chaining:

SessionFactory sf = new MetadataSources( serviceRegistry )
         .addResource( "some.hbm.xml" )
         .addAnnotatedClass( SomeEntity.class )
         .buildMetadata()
         .buildSessionFactory();



-- 
Steve Ebersole <steve at hibernate.org>
http://hibernate.org



More information about the hibernate-dev mailing list