forked from hibernate/hibernate-orm
/
DefaultIdentifierGeneratorFactory.java
141 lines (130 loc) · 5.09 KB
/
DefaultIdentifierGeneratorFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.id.factory;
import java.util.Properties;
import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.UUIDGenerator;
import org.hibernate.id.UUIDHexGenerator;
import org.hibernate.id.TableHiLoGenerator;
import org.hibernate.id.Assigned;
import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.SelectGenerator;
import org.hibernate.id.SequenceGenerator;
import org.hibernate.id.SequenceHiLoGenerator;
import org.hibernate.id.IncrementGenerator;
import org.hibernate.id.ForeignGenerator;
import org.hibernate.id.GUIDGenerator;
import org.hibernate.id.SequenceIdentityGenerator;
import org.hibernate.id.Configurable;
import org.hibernate.id.enhanced.SequenceStyleGenerator;
import org.hibernate.id.enhanced.TableGenerator;
import org.hibernate.type.Type;
import org.hibernate.util.ReflectHelper;
import org.hibernate.dialect.Dialect;
import org.hibernate.MappingException;
/**
* Basic <tt>templated</tt> support for {@link IdentifierGeneratorFactory} implementations.
*
* @author Steve Ebersole
*/
public class DefaultIdentifierGeneratorFactory implements IdentifierGeneratorFactory, Serializable {
private static final Logger log = LoggerFactory.getLogger( DefaultIdentifierGeneratorFactory.class );
private transient Dialect dialect;
private ConcurrentHashMap<String, Class> generatorStrategyToClassNameMap = new ConcurrentHashMap<String, Class>();
/**
* Constructs a new DefaultIdentifierGeneratorFactory.
*/
public DefaultIdentifierGeneratorFactory() {
register( "uuid2", UUIDGenerator.class );
register( "guid", GUIDGenerator.class ); // can be done with UUIDGenerator + strategy
register( "uuid", UUIDHexGenerator.class ); // "deprecated" for new use
register( "uuid.hex", UUIDHexGenerator.class ); // uuid.hex is deprecated
register( "hilo", TableHiLoGenerator.class );
register( "assigned", Assigned.class );
register( "identity", IdentityGenerator.class );
register( "select", SelectGenerator.class );
register( "sequence", SequenceGenerator.class );
register( "seqhilo", SequenceHiLoGenerator.class );
register( "increment", IncrementGenerator.class );
register( "foreign", ForeignGenerator.class );
register( "sequence-identity", SequenceIdentityGenerator.class );
register( "enhanced-sequence", SequenceStyleGenerator.class );
register( "enhanced-table", TableGenerator.class );
}
/**
* {@inheritDoc}
*/
public void setDialect(Dialect dialect) {
log.debug( "Setting dialect [" + dialect + "]" );
this.dialect = dialect;
}
public void register(String strategy, Class generatorClass) {
String msg = "Registering IdentifierGenerator strategy [" + strategy + "] -> [" + generatorClass + "]";
Object old = generatorStrategyToClassNameMap.put( strategy, generatorClass );
if ( old != null ) {
msg += ", overriding [" + old + "]";
}
log.debug( msg );
}
/**
* {@inheritDoc}
*/
public IdentifierGenerator createIdentifierGenerator(String strategy, Type type, Properties config) {
try {
Class clazz = getIdentifierGeneratorClass( strategy );
IdentifierGenerator idgen = ( IdentifierGenerator ) clazz.newInstance();
if ( idgen instanceof Configurable ) {
( ( Configurable ) idgen ).configure( type, config, dialect );
}
return idgen;
}
catch ( Exception e ) {
String msg = "Could not instantiate id generator [entity-name="
+ config.get( IdentifierGenerator.ENTITY_NAME ) + "]";
throw new MappingException( msg, e );
}
}
/**
* {@inheritDoc}
*/
public Class getIdentifierGeneratorClass(String strategy) {
if ( "native".equals( strategy ) ) {
return dialect.getNativeIdentifierGeneratorClass();
}
Class generatorClass = generatorStrategyToClassNameMap.get( strategy );
try {
if ( generatorClass == null ) {
generatorClass = ReflectHelper.classForName( strategy );
}
}
catch ( ClassNotFoundException e ) {
throw new MappingException( "Could not interpret id generator strategy [" + strategy + "]" );
}
return generatorClass;
}
}