Project

General

Profile

Download (3.24 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
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.
8
 */
9
package eu.etaxonomy.cdm.persistence.hibernate;
10

    
11
import java.util.Properties;
12

    
13
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
14
import org.hibernate.MappingException;
15
import org.hibernate.service.ServiceRegistry;
16
import org.hibernate.type.Type;
17

    
18
/**
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:
29
 *
30
 *<pre>
31
 * &lt;bean id=&quot;tableGeneratorGlobalOverride&quot; class=&quot;eu.etaxonomy.cdm.persistence.hibernate.TableGeneratorGlobalOverride&quot;&gt;
32
 *          &lt;property name=&quot;properties&quot;&gt;
33
	*            &lt;props&gt;
34
	*				&lt;!--
35
	*					globally overriding id generation settings
36
	*					see: eu.etaxonomy.cdm.persistence.hibernate.TableGenerator
37
	*				--&gt;
38
	*				&lt;prop key=&quot;optimizer&quot;&gt;none&lt;/prop&gt;
39
	*				&lt;prop key=&quot;initial_value&quot;&gt;1000&lt;/prop&gt;
40
 *          	&lt;/props&gt;
41
 *        &lt;/property&gt;
42
 *  &lt;/bean&gt;
43
 *</pre>
44
 *
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:
47
 *
48
 * <pre>
49
 * &lt;bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate5.LocalSessionFactoryBean&quot; depends-on=&quot;tableGeneratorGlobalOverride&quot;&gt;
50
 * ...
51
 * </pre>
52
 *
53
 * @author Andreas Kohlbecker, 2012
54
 */
55
//TODO this class has been moved to cdmlib-persistence preliminary. 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 {
61

    
62
	private static final Logger logger = LogManager.getLogger(TableGenerator.class);
63

    
64
	@Override
65
    public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
66

    
67
		Properties overrideProperies = TableGeneratorGlobalOverride.getProperties();
68
		if(overrideProperies != null) {
69
			params.putAll(overrideProperies);
70
		}
71
		logger.debug("overrideProperies:" + (overrideProperies != null ? overrideProperies :"NULL"));
72
		super.configure(type, params, serviceRegistry);
73
	}
74

    
75
}
(17-17/21)