001/* 002 * Copyright 2002-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jdbc.support.incrementer; 018 019import javax.sql.DataSource; 020 021/** 022 * {@link DataFieldMaxValueIncrementer} that increments the maximum value of a given Derby table 023 * with the equivalent of an auto-increment column. Note: If you use this class, your Derby key 024 * column should <i>NOT</i> be defined as an IDENTITY column, as the sequence table does the job. 025 * 026 * <p>The sequence is kept in a table. There should be one sequence table per 027 * table that needs an auto-generated key. 028 * 029 * <p>Derby requires an additional column to be used for the insert since it is impossible 030 * to insert a null into the identity column and have the value generated. This is solved by 031 * providing the name of a dummy column that also must be created in the sequence table. 032 * 033 * <p>Example: 034 * 035 * <pre class="code">create table tab (id int not null primary key, text varchar(100)); 036 * create table tab_sequence (value int generated always as identity, dummy char(1)); 037 * insert into tab_sequence (dummy) values(null);</pre> 038 * 039 * If "cacheSize" is set, the intermediate values are served without querying the 040 * database. If the server or your application is stopped or crashes or a transaction 041 * is rolled back, the unused values will never be served. The maximum hole size in 042 * numbering is consequently the value of cacheSize. 043 * 044 * <b>HINT:</b> Since Derby supports the JDBC 3.0 {@code getGeneratedKeys} method, 045 * it is recommended to use IDENTITY columns directly in the tables and then utilizing 046 * a {@link org.springframework.jdbc.support.KeyHolder} when calling the with the 047 * {@code update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder)} 048 * method of the {@link org.springframework.jdbc.core.JdbcTemplate}. 049 * 050 * <p>Thanks to Endre Stolsvik for the suggestion! 051 * 052 * @author Thomas Risberg 053 * @author Juergen Hoeller 054 * @since 2.5 055 */ 056public class DerbyMaxValueIncrementer extends AbstractIdentityColumnMaxValueIncrementer { 057 058 /** The default for dummy name */ 059 private static final String DEFAULT_DUMMY_NAME = "dummy"; 060 061 /** The name of the dummy column used for inserts */ 062 private String dummyName = DEFAULT_DUMMY_NAME; 063 064 065 /** 066 * Default constructor for bean property style usage. 067 * @see #setDataSource 068 * @see #setIncrementerName 069 * @see #setColumnName 070 */ 071 public DerbyMaxValueIncrementer() { 072 } 073 074 /** 075 * Convenience constructor. 076 * @param dataSource the DataSource to use 077 * @param incrementerName the name of the sequence/table to use 078 * @param columnName the name of the column in the sequence table to use 079 */ 080 public DerbyMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName) { 081 super(dataSource, incrementerName, columnName); 082 this.dummyName = DEFAULT_DUMMY_NAME; 083 } 084 085 /** 086 * Convenience constructor. 087 * @param dataSource the DataSource to use 088 * @param incrementerName the name of the sequence/table to use 089 * @param columnName the name of the column in the sequence table to use 090 * @param dummyName the name of the dummy column used for inserts 091 */ 092 public DerbyMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName, String dummyName) { 093 super(dataSource, incrementerName, columnName); 094 this.dummyName = dummyName; 095 } 096 097 098 /** 099 * Set the name of the dummy column. 100 */ 101 public void setDummyName(String dummyName) { 102 this.dummyName = dummyName; 103 } 104 105 /** 106 * Return the name of the dummy column. 107 */ 108 public String getDummyName() { 109 return this.dummyName; 110 } 111 112 113 @Override 114 protected String getIncrementStatement() { 115 return "insert into " + getIncrementerName() + " (" + getDummyName() + ") values(null)"; 116 } 117 118 @Override 119 protected String getIdentityStatement() { 120 return "select IDENTITY_VAL_LOCAL() from " + getIncrementerName(); 121 } 122 123}