2
* Hibernate, Relational Persistence for Idiomatic Java
4
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5
* indicated by the @author tags or express copyright attribution
6
* statements applied by the authors. All third-party contributions are
7
* distributed under license by Red Hat Middleware LLC.
9
* This copyrighted material is made available to anyone wishing to use, modify,
10
* copy, or redistribute it subject to the terms and conditions of the GNU
11
* Lesser General Public License, as published by the Free Software Foundation.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with this distribution; if not, write to:
20
* Free Software Foundation, Inc.
21
* 51 Franklin Street, Fifth Floor
22
* Boston, MA 02110-1301 USA
25
package org.hibernate.id;
27
import java.io.Serializable;
28
import java.sql.PreparedStatement;
29
import java.sql.ResultSet;
30
import java.sql.SQLException;
31
import java.util.Properties;
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35
import org.hibernate.HibernateException;
36
import org.hibernate.MappingException;
37
import org.hibernate.exception.JDBCExceptionHelper;
38
import org.hibernate.dialect.Dialect;
39
import org.hibernate.engine.SessionImplementor;
40
import org.hibernate.mapping.Table;
41
import org.hibernate.type.Type;
42
import org.hibernate.util.PropertiesHelper;
47
* Generates <tt>long</tt> values using an oracle-style sequence. A higher
48
* performance algorithm is <tt>SequenceHiLoGenerator</tt>.<br>
50
* Mapping parameters supported: sequence, parameters.
52
* @see SequenceHiLoGenerator
53
* @see TableHiLoGenerator
57
public class SequenceGenerator implements PersistentIdentifierGenerator, Configurable {
60
* The sequence parameter
62
public static final String SEQUENCE = "sequence";
65
* The parameters parameter, appended to the create sequence DDL.
66
* For example (Oracle): <tt>INCREMENT BY 1 START WITH 1 MAXVALUE 100 NOCACHE</tt>.
68
public static final String PARAMETERS = "parameters";
70
private String sequenceName;
71
private String parameters;
72
private Type identifierType;
75
private static final Logger log = LoggerFactory.getLogger(SequenceGenerator.class);
77
public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
78
sequenceName = PropertiesHelper.getString(SEQUENCE, params, "hibernate_sequence");
79
parameters = params.getProperty(PARAMETERS);
80
String schemaName = params.getProperty(SCHEMA);
81
String catalogName = params.getProperty(CATALOG);
83
if (sequenceName.indexOf( '.' ) < 0) {
84
sequenceName = Table.qualify( catalogName, schemaName, sequenceName );
87
this.identifierType = type;
88
sql = dialect.getSequenceNextValString(sequenceName);
91
public Serializable generate(SessionImplementor session, Object obj)
92
throws HibernateException {
96
PreparedStatement st = session.getBatcher().prepareSelectStatement(sql);
98
ResultSet rs = st.executeQuery();
101
Serializable result = IdentifierGeneratorFactory.get(
104
if ( log.isDebugEnabled() ) {
105
log.debug("Sequence identifier generated: " + result);
114
session.getBatcher().closeStatement(st);
118
catch (SQLException sqle) {
119
throw JDBCExceptionHelper.convert(
120
session.getFactory().getSQLExceptionConverter(),
122
"could not get next sequence value",
129
public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
130
String[] ddl = dialect.getCreateSequenceStrings(sequenceName);
131
if ( parameters != null ) {
132
ddl[ddl.length - 1] += ' ' + parameters;
137
public String[] sqlDropStrings(Dialect dialect) throws HibernateException {
138
return dialect.getDropSequenceStrings(sequenceName);
141
public Object generatorKey() {
145
public String getSequenceName() {