4 package eu
.etaxonomy
.cdm
.persistence
.hibernate
;
6 import java
.util
.Properties
;
8 import org
.apache
.log4j
.Logger
;
9 import org
.hibernate
.MappingException
;
10 import org
.hibernate
.dialect
.Dialect
;
11 import org
.hibernate
.type
.Type
;
14 * Subclass of the {@link org.hibernate.id.enhanced.TableGenerator} for the sole purpose to
15 * allow overriding the {@link org.hibernate.id.enhanced.TableGenerator#OPT_PARAM} and
16 * {@link org.hibernate.id.enhanced.TableGenerator#INITIAL_PARAM} for the testing environment
17 * (in principle you can override any of the TableGenerator parameters).
18 * Test data may not always contain the hibernate sequences table which often leads to problems
19 * with existing primary key values when inserting new entities. This especially occurs when
20 * running test in a suite.
21 * To circumvent these problems you can set a global override for the high initial parameter which
22 * is far beyond any id ever used in test data sets.
23 * You may want to set this in your testing application context, eg:
26 * <bean id="tableGeneratorGlobalOverride" class="eu.etaxonomy.cdm.persistence.hibernate.TableGeneratorGlobalOverride">
27 * <property name="properties">
30 * globally overriding id generation settings
31 * see: eu.etaxonomy.cdm.persistence.hibernate.TableGenerator
33 * <prop key="optimizer">none</prop>
34 * <prop key="initial_value">1000</prop>
40 * If you set the optimizer to "none", hibernate will always query the database for each new id.
41 * You must tell spring to intantiate the ... before the session factory:
44 * <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" depends-on="tableGeneratorGlobalOverride">
50 * @author Andreas Kohlbecker, 2012
53 public class TableGenerator
extends org
.hibernate
.id
.enhanced
.TableGenerator
{
56 private static final Logger logger
= Logger
.getLogger(TableGenerator
.class);
61 public void configure(Type type
, Properties params
, Dialect dialect
) throws MappingException
{
63 Properties overrideProperies
= TableGeneratorGlobalOverride
.getProperties();
64 if(overrideProperies
!= null) {
65 params
.putAll(overrideProperies
);
67 logger
.debug("overrideProperies:" + (overrideProperies
!= null ? overrideProperies
:"NULL"));
68 super.configure(type
, params
, dialect
);
75 // public synchronized Serializable generate(final SessionImplementor session, Object obj) {
77 // Serializable nextId = super.generate(session, obj);
78 // logger.debug("next id for " + obj.getClass().getSimpleName() + ":" + obj + " =" + nextId );
82 // if(nextId instanceof Number){
83 // long nextIdL = ((Number)nextId).longValue();
84 // int nextIdOffset = 1000;
85 // logger.info("next id = " + (nextIdL + nextIdOffset));
86 // return IdentifierGeneratorHelper.createNumber( nextIdL + nextIdOffset, nextId.getClass() );
88 // logger.error("identifier expected to be a Number, cannot apply offset");