2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.persistence
.hibernate
;
11 import java
.util
.Properties
;
13 import org
.apache
.log4j
.Logger
;
14 import org
.hibernate
.MappingException
;
15 import org
.hibernate
.service
.ServiceRegistry
;
16 import org
.hibernate
.type
.Type
;
19 * Subclass of the {@link org.hibernate.id.enhanced.TableGenerator} for the sole purpose to
20 * allow overriding the {@link org.hibernate.id.enhanced.TableGenerator#OPT_PARAM} and
21 * {@link org.hibernate.id.enhanced.TableGenerator#INITIAL_PARAM} for the testing environment
22 * (in principle you can override any of the TableGenerator parameters).
23 * Test data may not always contain the hibernate sequences table which often leads to problems
24 * with existing primary key values when inserting new entities. This especially occurs when
25 * running test in a suite.
26 * To circumvent these problems you can set a global override for the high initial parameter which
27 * is far beyond any id ever used in test data sets.
28 * You may want to set this in your testing application context, eg:
31 * <bean id="tableGeneratorGlobalOverride" class="eu.etaxonomy.cdm.persistence.hibernate.TableGeneratorGlobalOverride">
32 * <property name="properties">
35 * globally overriding id generation settings
36 * see: eu.etaxonomy.cdm.persistence.hibernate.TableGenerator
38 * <prop key="optimizer">none</prop>
39 * <prop key="initial_value">1000</prop>
45 * If you set the optimizer to "none", hibernate will always query the database for each new id.
46 * You must tell spring to instantiate the ... before the session factory:
49 * <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" depends-on="tableGeneratorGlobalOverride">
53 * @author Andreas Kohlbecker, 2012
55 //TODO this class has been moved to cdmlib-persistence preliminarily. It should be moved to
56 //cdmlib-test again as it should be used only in test. Currently this is not possible because
57 //sessionFactory bean has a dependsOn relationship to this class and this creates problems in remote-webapp
58 //as the been is not available.
59 //see also TableGeneratorGlobalOverride
60 public class TableGenerator
extends org
.hibernate
.id
.enhanced
.TableGenerator
{
62 private static final Logger logger
= Logger
.getLogger(TableGenerator
.class);
65 public void configure(Type type
, Properties params
, ServiceRegistry serviceRegistry
) throws MappingException
{
67 Properties overrideProperies
= TableGeneratorGlobalOverride
.getProperties();
68 if(overrideProperies
!= null) {
69 params
.putAll(overrideProperies
);
71 logger
.debug("overrideProperies:" + (overrideProperies
!= null ? overrideProperies
:"NULL"));
72 super.configure(type
, params
, serviceRegistry
);