Data Model Configuration

OSGI Model Service

The Data Base Classes deliver an OSGI service that allow a flexible and easy-to-use configuration of your data model. Of course, you could decide not to use the XML-based configuration for your data model. However, the benefits of it cannot be underestimated especially when your application users require configuration.

The IOsgiModelService can be used to load your data model and to manage multiple data models or DAO factories. It is very easy to use:

1
2
3
4
5
6
7
8
9
10
11
// Get the Model service
IOsgiModelService modelService = OsgiModelServiceImpl.getModelService();
 
// Register a factory
modelService.registerFactory("myFactory", myModelFactory);
 
// Retrieving a factory
MyFactory aFactory = (MyFactory)modelService.getFactory("myFactory");
 
// 2nd way of getting the factory
MyFactory aFactory = modelService.getFactory(MyFactory.class);

The above code demonstrates how to get the service itself (line 2) and how to register your factory (line 5). Getting a factory can be done either by the registered name (line 8) or by the class that you expect (line 11). Latter option doesn’t require a class cast but you need to be sure that your factory is the only one of the given class in the service.

However, you don’t need to register your factory yourself. There is an easier way to instantiate and register it – the DAO Configuration File.

DAO Configuration File

The implementations delivered with the Data Base Classes can be completely configured using XML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<config>
   <!-- DAO Factory 1 -->
   <DaoFactory class="my.package.ExampleDaoFactoryImpl" name="myFactory">
 
      ...
 
   </DaoFactory>
 
   <!-- DAO Factory 2 -->
   <DaoFactory class="my.package.AnotherDaoFactoryImpl" name="anotherFactory">
 
      ...
 
   </DaoFactory>
</config>

The above XML file defines two different DAO factories: myFactory and anotherFactory. If your XML file is called dao-config.xml and located in the classpath of your application, then it can be loaded automatically by the OSGI service:

1
2
3
4
5
6
// Get the Model service
IOsgiModelService modelService = OsgiModelServiceImpl.getModelService();
 
// Retrieving our factories
ExampleDaoFactory factory1 = modelService.getFactory(ExampleDaoFactory.class);
AnotherDaoFactory factory1 = modelService.getFactory(AnotherDaoFactory.class);

Otherwise, just tell the service where to find your factory:

1
2
3
4
5
6
7
8
9
// Get the Model service
IOsgiModelService modelService = OsgiModelServiceImpl.getModelService();
 
// Load the configuration file
modelService.setConfiguration(new XMLConfiguration("/path/to/dao-config.xml"));
 
// Retrieving our factories
ExampleDaoFactory factory1 = modelService.getFactory(ExampleDaoFactory.class);
AnotherDaoFactory factory1 = modelService.getFactory(AnotherDaoFactory.class);

Configuring a DAO Factory

The delivered implementation of a DAO Factory can load all its required data from the DAO Configuration File. Two parts are required: DAO Master definitions and DAOs itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<config>
   <DaoFactory class="my.package.ExampleDaoFactoryImpl" name="myFactory">
 
      <DaoMaster class="my.package.MyDaoMaster" name="master1">
         ...
      </DaoMaster>
      <DaoMaster class="my.package.AnotherDaoMaster2" name="master2">
         ...
      </DaoMaster>
 
      <!-- DAO definitions -->
      <Dao class="my.package.CustomerDAOImpl" daoMaster="master1"/>
      <Dao class="my.package.ProductDAOImpl" daoMaster="master2"/>
 
   </DaoFactory>
</config>

DAO Masters hold shared information across multiple DAOs. There is no need to have such DAO Master, but it can be useful especially when you need to share login or configuration information. The example above defines to DAO Masters called master1 and master2. A DAO Master that does not have any name assigned is automatically called default.

The DAO definition is very forward. You name your DAO Implementation class and the name of the DAO Master that belongs to this DAO. If you omit the DAO Master name, default is assumed. If no such master exists then your DAO will not have any DAO Master assigned.

HibernateDaoMaster

The Data Hibernate Classes deliver a ready-to-use HibernateDaoMaster. Its implementation separates model definition (through Hibernate configuration file) and database access information. This enables you to have the model delivered with your application while the users just need to configure what database they want to use.

1
2
3
4
5
6
7
8
9
10
11
12
13
<config>
   <DaoFactory class="my.package.ExampleDaoFactoryImpl" name="myFactory">
 
      <DaoMaster class="rs.data.hibernate.HibernateDaoMaster">
         <!-- Parameters -->
         <property name="dbconfig-file">dbconfig.xml</property>
         <property name="hbmconfig-file">hibernate.cfg.xml</property>
      </DaoMaster>
 
      ...
 
   </DaoFactory>
</config>

Database Access Information

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<dbconfig>
	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
 
	<datasource class="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource">
		<property name="url">jdbc:mysql://db-host:3306/db-name</property>
		<property name="user">db-user</property>
		<property name="password">db-password</property>
        </datasource>
</dbconfig>

The file can contain additional Hibernate configurations (as in line 3). Most important is the SQL dialect that Hibernate requires.

HibernateDaoMaster assumes that the class given in the <datasource> tag is a XADataSource. You need to refer to the documentation of your class in order to configure that source properly.

Hibernate Configuration File

This file is a standard HBM file with the exception of any DB access information. However, if you have such information in there, it will be overridden by the data from the Database Access Information. The following example is taken from one of my other projects using the Data Hibernate Classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<hibernate-configuration>
   <session-factory>
      <!-- How to find the Transaction -->
      <property name="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</property>
 
      <!-- JTA Platform definition -->
      <property name="hibernate.transaction.jta.platform">rs.data.hibernate.util.JOTMJtaPlatform</property>
 
      <!-- Session context with JTA -->
      <property name="current_session_context_class">jta</property>
 
      <!-- Isolation level -->
      <property name="hibernate.connection.isolation">2</property>
 
      <!-- Performance speedup -->
      <property name="hibernate.jdbc.batch_size">40</property>
 
      <!-- Show SQL properties -->
      <property name="hibernate.show_sql">false</property>
      <property name="hibernate.format_sql">true</property>
 
      <!-- Required to enable JOTM with Hibernate -->
      <property name="hibernate.connection.provider_class">rs.data.hibernate.util.DataSourceConnectionProvider</property>
 
      <!-- Class Mapping Files -->
      <mapping resource="customer.hbm.xml"/>
      ...
   <session-factory>
</hibernate-configuration>

One Response to “Data Model Configuration”

  1. American Steroids Online Says:

    […] with Bitcoins Buy American Steroids Online with Bitcoins Buy American Steroids Online with Bitcoins Buy American Steroids Online with Bitcoins Buy American Steroids Online with Bitcoins Buy American Steroids Online with Bitcoins Buy American […]

Leave a Reply