HIBERNATE JBoss.org
 |  Register  | 
     
News 
About 
   Feature List 
   Road Map 
Documentation 
   Related Projects 
   External Documentation 
Download 
Forum & Mailinglists 
Support & Training 
JIRA Issue Tracking
Wiki Community Area


Hibernate Public Training Courses


Get Hibernate in Action eBook!


JavaWorld 2003 Finalist


Jolt Award 2004 Winner
      
Documentation > Community Area > Hibernate and dynamic models

Hibernate and dynamic models

Generality

Hibernate is well designed to manage defined and static models. But there are several ways to manage dynamic models. First of all, some concepts ; there are several kind of dynamic models:

  • mapping/configuration changing between 2 executions of your application
  • mapping/configuration changing during the lifetime of your application but without changes expected in the current Sessions
  • mapping/configuration changing during the lifetime of your application and affecting current Sessions

The first and the second can be achieve with Hibernate. There is also another dynamic way which is generic classes driven by mapping.

Dynamic mapping and SessionFactory / Session lifetime

Hibernate 2.1 provides a way to dynamically build the meta model.

But first of all, once a SessionFactory is built (Configuration.buildSessionFactory()), there is no way to change it's mapping/configuration at runtime. This is not expected to change in a near future, if ever. Thus Session mapping/configuration is also frozen for the session's lifetime (note: this does not really matters since Session should be short-life objects).

So changing the mapping/configuration need to build a new SessionFactory and new Sessions.

Managing configurations

Configuration can be achieved by several ways:

  • using -D option in the JVM command line (read one time during Hibernate init)
  • using hibernate.properties (read one time during Hibernate init)
  • using hibernate.cfg.xml (read before SessionFactory build)
  • using the API (read before SessionFactory build)

The first 3 are the most used. Reading configuration dynamically is achieved by the API way using:

  • configuration.configure(File), reading the resource you've provided
  • configuration.configure(String), reading the resource you've provided (String is the resource path)
  • configuration.configure(URL), reading the resource you've provided
  • configuration.configure(DOM), reading the Document Object Model you've provided
  • configuration.addProperties(), reading properties from a Properties object.

That way, the first three allows you to read configuration from where you want, the last ones allows you to build programatically the configuration you need.

Managing mappings

Once the previous configuration is done and before the SessionFactory is build, we can manage the mapping.

Here are the different way to manage it:

  • Adding a resource (addClass, addDirectory, addFile, addImputStream, addJar, addResource, addURL)
  • Adding a home made mapping (addDocument to add a DOM, =addXML, to add an XML stream)
  • Building a configuration mapping programmatically using configuration.createMappings() and using the net.sf.hibernate.mapping package

The first ones are basically readers on top of the hbm.xml files.The second ones allow you to build DOM or XML strings the way you want. The third ones allow you to build a mapping programmatically (this is the most complex way to do it).

Generic classes driven by mapping

All previous topics assume the object model were built on to of defined classes. You can of course build your classes dynamically using some byte-code manipulation.

Hibernate provide a way to use classes in a more generic way using:

  • dynamic-component
  • dynamic-class (2.2 feature)

The dynamic-component allows you to define you component properties into your mapping file and to map this in a Map object instead of a specific component class. See the reference guide for more information.

The new feature dynamic-class allows you to do the same for classes. This feature will be available in the 2.2 version (check the CVS v22branch for more details).

      

coWiki