Revision b705e3fa
Added by Andreas Müller about 8 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/TableGenerator.java | ||
---|---|---|
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.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 |
* <bean id="tableGeneratorGlobalOverride" class="eu.etaxonomy.cdm.persistence.hibernate.TableGeneratorGlobalOverride"> |
|
32 |
* <property name="properties"> |
|
33 |
* <props> |
|
34 |
* <!-- |
|
35 |
* globally overriding id generation settings |
|
36 |
* see: eu.etaxonomy.cdm.persistence.hibernate.TableGenerator |
|
37 |
* --> |
|
38 |
* <prop key="optimizer">none</prop> |
|
39 |
* <prop key="initial_value">1000</prop> |
|
40 |
* </props> |
|
41 |
* </property> |
|
42 |
* </bean> |
|
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 |
* <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" depends-on="tableGeneratorGlobalOverride"> |
|
50 |
* ... |
|
51 |
* </pre> |
|
52 |
* |
|
53 |
* |
|
54 |
* |
|
55 |
* @author Andreas Kohlbecker, 2012 |
|
56 |
* |
|
57 |
*/ |
|
58 |
//TODO this class has been moved to cdmlib-persistence preliminarily. It should be moved to |
|
59 |
//cdmlib-test again as it should be used only in test. Currently this is not possible because |
|
60 |
//sessionFactory bean has a dependsOn relationship to this class |
|
61 |
//see also TableGeneratorGlobalOverride |
|
62 |
public class TableGenerator extends org.hibernate.id.enhanced.TableGenerator { |
|
63 |
|
|
64 |
|
|
65 |
private static final Logger logger = Logger.getLogger(TableGenerator.class); |
|
66 |
|
|
67 |
/** |
|
68 |
* {@inheritDoc} |
|
69 |
*/ |
|
70 |
@Override |
|
71 |
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException { |
|
72 |
|
|
73 |
Properties overrideProperies = TableGeneratorGlobalOverride.getProperties(); |
|
74 |
if(overrideProperies != null) { |
|
75 |
params.putAll(overrideProperies); |
|
76 |
} |
|
77 |
logger.debug("overrideProperies:" + (overrideProperies != null ? overrideProperies :"NULL")); |
|
78 |
super.configure(type, params, serviceRegistry); |
|
79 |
} |
|
80 |
|
|
81 |
// /** |
|
82 |
// * {@inheritDoc} |
|
83 |
// */ |
|
84 |
// @Override |
|
85 |
// public synchronized Serializable generate(final SessionImplementor session, Object obj) { |
|
86 |
// |
|
87 |
// Serializable nextId = super.generate(session, obj); |
|
88 |
// logger.debug("next id for " + obj.getClass().getSimpleName() + ":" + obj + " =" + nextId ); |
|
89 |
// return nextId; |
|
90 |
// |
|
91 |
// /* |
|
92 |
// if(nextId instanceof Number){ |
|
93 |
// long nextIdL = ((Number)nextId).longValue(); |
|
94 |
// int nextIdOffset = 1000; |
|
95 |
// logger.info("next id = " + (nextIdL + nextIdOffset)); |
|
96 |
// return IdentifierGeneratorHelper.createNumber( nextIdL + nextIdOffset, nextId.getClass() ); |
|
97 |
// } else { |
|
98 |
// logger.error("identifier expected to be a Number, cannot apply offset"); |
|
99 |
// return nextId; |
|
100 |
// } |
|
101 |
// */ |
|
102 |
// } |
|
103 |
|
|
104 |
} |
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/TableGeneratorGlobalOverride.java | ||
---|---|---|
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.springframework.stereotype.Component; |
|
14 |
|
|
15 |
/** |
|
16 |
* This class allows to globally override id generation settings in hibernate |
|
17 |
* |
|
18 |
* @see: eu.etaxonomy.cdm.persistence.hibernate.TableGenerator |
|
19 |
* |
|
20 |
* @author a.kohlbecker |
|
21 |
* @date Feb 23, 2016 |
|
22 |
* |
|
23 |
*/ |
|
24 |
//TODO this class has been moved to cdmlib-persistence preliminarily. It should be moved to |
|
25 |
//cdmlib-test again as it should be used only in test. Currently this is not possible because |
|
26 |
//sessionFactory bean has a dependsOn relationship to this class |
|
27 |
//see also TableGenerator |
|
28 |
@Component |
|
29 |
class TableGeneratorGlobalOverride { |
|
30 |
|
|
31 |
public TableGeneratorGlobalOverride(){} |
|
32 |
|
|
33 |
// public static final ThreadLocal<Properties> threadLocalProperties = new ThreadLocal<Properties>(); |
|
34 |
|
|
35 |
public static Properties properties; |
|
36 |
|
|
37 |
public static Properties getProperties() { |
|
38 |
// return threadLocalProperties.get(); |
|
39 |
return TableGeneratorGlobalOverride.properties; |
|
40 |
} |
|
41 |
|
|
42 |
public void setProperties(Properties properties) { |
|
43 |
// threadLocalProperties.set(properties); |
|
44 |
TableGeneratorGlobalOverride.properties = properties; |
|
45 |
} |
|
46 |
|
|
47 |
} |
cdmlib-test/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/TableGenerator.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package eu.etaxonomy.cdm.persistence.hibernate; |
|
5 |
|
|
6 |
import java.util.Properties; |
|
7 |
|
|
8 |
import org.apache.log4j.Logger; |
|
9 |
import org.hibernate.MappingException; |
|
10 |
import org.hibernate.service.ServiceRegistry; |
|
11 |
import org.hibernate.type.Type; |
|
12 |
|
|
13 |
/** |
|
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: |
|
24 |
* |
|
25 |
*<pre> |
|
26 |
* <bean id="tableGeneratorGlobalOverride" class="eu.etaxonomy.cdm.persistence.hibernate.TableGeneratorGlobalOverride"> |
|
27 |
* <property name="properties"> |
|
28 |
* <props> |
|
29 |
* <!-- |
|
30 |
* globally overriding id generation settings |
|
31 |
* see: eu.etaxonomy.cdm.persistence.hibernate.TableGenerator |
|
32 |
* --> |
|
33 |
* <prop key="optimizer">none</prop> |
|
34 |
* <prop key="initial_value">1000</prop> |
|
35 |
* </props> |
|
36 |
* </property> |
|
37 |
* </bean> |
|
38 |
*</pre> |
|
39 |
* |
|
40 |
* If you set the optimizer to "none", hibernate will always query the database for each new id. |
|
41 |
* You must tell spring to instantiate the ... before the session factory: |
|
42 |
* |
|
43 |
* <pre> |
|
44 |
* <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" depends-on="tableGeneratorGlobalOverride"> |
|
45 |
* ... |
|
46 |
* </pre> |
|
47 |
* |
|
48 |
* |
|
49 |
* |
|
50 |
* @author Andreas Kohlbecker, 2012 |
|
51 |
* |
|
52 |
*/ |
|
53 |
public class TableGenerator extends org.hibernate.id.enhanced.TableGenerator { |
|
54 |
|
|
55 |
|
|
56 |
private static final Logger logger = Logger.getLogger(TableGenerator.class); |
|
57 |
|
|
58 |
/** |
|
59 |
* {@inheritDoc} |
|
60 |
*/ |
|
61 |
@Override |
|
62 |
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException { |
|
63 |
|
|
64 |
Properties overrideProperies = TableGeneratorGlobalOverride.getProperties(); |
|
65 |
if(overrideProperies != null) { |
|
66 |
params.putAll(overrideProperies); |
|
67 |
} |
|
68 |
logger.debug("overrideProperies:" + (overrideProperies != null ? overrideProperies :"NULL")); |
|
69 |
super.configure(type, params, serviceRegistry); |
|
70 |
} |
|
71 |
|
|
72 |
// /** |
|
73 |
// * {@inheritDoc} |
|
74 |
// */ |
|
75 |
// @Override |
|
76 |
// public synchronized Serializable generate(final SessionImplementor session, Object obj) { |
|
77 |
// |
|
78 |
// Serializable nextId = super.generate(session, obj); |
|
79 |
// logger.debug("next id for " + obj.getClass().getSimpleName() + ":" + obj + " =" + nextId ); |
|
80 |
// return nextId; |
|
81 |
// |
|
82 |
// /* |
|
83 |
// if(nextId instanceof Number){ |
|
84 |
// long nextIdL = ((Number)nextId).longValue(); |
|
85 |
// int nextIdOffset = 1000; |
|
86 |
// logger.info("next id = " + (nextIdL + nextIdOffset)); |
|
87 |
// return IdentifierGeneratorHelper.createNumber( nextIdL + nextIdOffset, nextId.getClass() ); |
|
88 |
// } else { |
|
89 |
// logger.error("identifier expected to be a Number, cannot apply offset"); |
|
90 |
// return nextId; |
|
91 |
// } |
|
92 |
// */ |
|
93 |
// } |
|
94 |
|
|
95 |
} |
cdmlib-test/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/TableGeneratorGlobalOverride.java | ||
---|---|---|
1 |
package eu.etaxonomy.cdm.persistence.hibernate; |
|
2 |
|
|
3 |
import java.util.Properties; |
|
4 |
|
|
5 |
import org.springframework.stereotype.Component; |
|
6 |
|
|
7 |
/** |
|
8 |
* This class allows to globally override id generation settings in hibernate |
|
9 |
* |
|
10 |
* @see: eu.etaxonomy.cdm.persistence.hibernate.TableGenerator |
|
11 |
* |
|
12 |
* @author a.kohlbecker |
|
13 |
* @date Feb 23, 2016 |
|
14 |
* |
|
15 |
*/ |
|
16 |
@Component |
|
17 |
class TableGeneratorGlobalOverride { |
|
18 |
|
|
19 |
public TableGeneratorGlobalOverride(){} |
|
20 |
|
|
21 |
// public static final ThreadLocal<Properties> threadLocalProperties = new ThreadLocal<Properties>(); |
|
22 |
|
|
23 |
public static Properties properties; |
|
24 |
|
|
25 |
public static Properties getProperties() { |
|
26 |
// return threadLocalProperties.get(); |
|
27 |
return TableGeneratorGlobalOverride.properties; |
|
28 |
} |
|
29 |
|
|
30 |
public void setProperties(Properties properties) { |
|
31 |
// threadLocalProperties.set(properties); |
|
32 |
TableGeneratorGlobalOverride.properties = properties; |
|
33 |
} |
|
34 |
|
|
35 |
} |
Also available in: Unified diff
Move TableGenerator(GlobalOverride) from test to persistence #4716