Child pages
  • Use of HiLoGenerator with sql-util DAO classes

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: cleaned up idGenerator/sqlGenerator mixup, info about upcoming pseudo constants in HiLoIdGen.

Add table ID_GENERATOR to TableDefinitions

 

TableInfo idGenerator=new TableInfo("ID_GENERATOR");
idGenerator.setColumns(Arrays.asList(
               new ColumnInfo("NEXT_ID",Types.BIGINT,20,null,false,null)
));

Future versions should use the constants HiLoIdGenerator.DEFAULT_ID_TABLE_NAMEHiLoIdGenerator.DEFAULT_NEXT_ID_COL_NAME which are part of sql-util as of the yet unreleased version 1.2.1.

Instantiate an idGenerator bean

<bp:bean id="idGenerator" class="org.clazzes.util.sql.dao.HiLoIdGenerator"
         init-method="initialize" depends-on="databaseSetup">
  <bp:property name="dataSource" ref="dataSource"/>

 

databaseSetup is the bean representing the JDBC2XML SchemaManager (see this HowTo), depends-on assures, that the ID generator is started after the database tables are set up.

Variant: idGenerator bean idGeneratorbean using application specific table and column names:

 

<bp:bean id="idGenerator" class="org.clazzes.util.sql.dao.HiLoIdGenerator"
         init-method="initialize" depends-on="databaseSetup">
  <bp:property name="dataSource" ref="dataSource"/>
  <!-- idTableName, default: ID_GENERATOR -->
  <bp:property name="idTableName" value="MYAPP_ID_GENERATOR"/>
  <!-- nextIdColumnName, default: NEXT_ID -->
 <bp:property name="nextIdColumnName" value="NEXT_ID"/>

 

Set the

...

sqlGenerator in your DAOs

 

<bp:bean id="userDAO" class="at.egv.drust.impl.dao.jdbc.DsUserJdbcDAO">
  <bp:property name="idGenerator" ref="idGenerator"/>
  <bp:property name="generator" ref=
"
sqlGenerator"
/>
  <bp:property name="threadLocalKey" ref="jdbcUrl"/>