merge trunk into cdm-3.3
[cdmlib.git] / cdmlib-persistence / 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.dialect.Dialect;
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 * &lt;bean id=&quot;tableGeneratorGlobalOverride&quot; class=&quot;eu.etaxonomy.cdm.persistence.hibernate.TableGeneratorGlobalOverride&quot;&gt;
27 * &lt;property name=&quot;properties&quot;&gt;
28 * &lt;props&gt;
29 * &lt;!--
30 * globally overriding id generation settings
31 * see: eu.etaxonomy.cdm.persistence.hibernate.TableGenerator
32 * --&gt;
33 * &lt;prop key=&quot;optimizer&quot;&gt;none&lt;/prop&gt;
34 * &lt;prop key=&quot;initial_value&quot;&gt;1000&lt;/prop&gt;
35 * &lt;/props&gt;
36 * &lt;/property&gt;
37 * &lt;/bean&gt;
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 intantiate the ... before the session factory:
42 *
43 * <pre>
44 * &lt;bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate4.LocalSessionFactoryBean&quot; depends-on=&quot;tableGeneratorGlobalOverride&quot;&gt;
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 public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
62
63 Properties overrideProperies = TableGeneratorGlobalOverride.getProperties();
64 if(overrideProperies != null) {
65 params.putAll(overrideProperies);
66 }
67 logger.debug("overrideProperies:" + (overrideProperies != null ? overrideProperies :"NULL"));
68 super.configure(type, params, dialect);
69 }
70
71 // /**
72 // * {@inheritDoc}
73 // */
74 // @Override
75 // public synchronized Serializable generate(final SessionImplementor session, Object obj) {
76 //
77 // Serializable nextId = super.generate(session, obj);
78 // logger.debug("next id for " + obj.getClass().getSimpleName() + ":" + obj + " =" + nextId );
79 // return nextId;
80 //
81 // /*
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() );
87 // } else {
88 // logger.error("identifier expected to be a Number, cannot apply offset");
89 // return nextId;
90 // }
91 // */
92 // }
93
94 }