| Method from org.hibernate.id.enhanced.TableGenerator Detail: |
protected String buildInsertQuery() {
return "insert into " + tableName + " (" + segmentColumnName + ", " + valueColumnName + ") " + " values (?,?)";
}
|
protected String buildSelectQuery(Dialect dialect) {
final String alias = "tbl";
String query = "select " + StringHelper.qualify( alias, valueColumnName ) +
" from " + tableName + ' " + alias +
" where " + StringHelper.qualify( alias, segmentColumnName ) + "=?";
HashMap lockMap = new HashMap();
lockMap.put( alias, LockMode.UPGRADE );
Map updateTargetColumnsMap = Collections.singletonMap( alias, new String[] { valueColumnName } );
return dialect.applyLocksToSql( query, lockMap, updateTargetColumnsMap );
}
|
protected String buildUpdateQuery() {
return "update " + tableName +
" set " + valueColumnName + "=? " +
" where " + valueColumnName + "=? and " + segmentColumnName + "=?";
}
|
public void configure(Type type,
Properties params,
Dialect dialect) throws MappingException {
identifierType = type;
tableName = determneGeneratorTableName( params );
segmentColumnName = determineSegmentColumnName( params );
valueColumnName = determineValueColumnName( params );
segmentValue = determineSegmentValue( params );
segmentValueLength = determineSegmentColumnSize( params );
initialValue = determineInitialValue( params );
incrementSize = determineIncrementSize( params );
this.selectQuery = buildSelectQuery( dialect );
this.updateQuery = buildUpdateQuery();
this.insertQuery = buildInsertQuery();
String defOptStrategy = incrementSize < = 1 ? OptimizerFactory.NONE : OptimizerFactory.POOL;
String optimizationStrategy = PropertiesHelper.getString( OPT_PARAM, params, defOptStrategy );
optimizer = OptimizerFactory.buildOptimizer( optimizationStrategy, identifierType.getReturnedClass(), incrementSize );
}
|
protected String determineDefaultSegmentValue(Properties params) {
boolean preferSegmentPerEntity = PropertiesHelper.getBoolean( CONFIG_PREFER_SEGMENT_PER_ENTITY, params, false );
String defaultToUse = preferSegmentPerEntity ? params.getProperty( TABLE ) : DEF_SEGMENT_VALUE;
log.info( "explicit segment value for id generator [" + tableName + '." + segmentColumnName + "] suggested; using default [" + defaultToUse + "]" );
return defaultToUse;
}
|
protected int determineIncrementSize(Properties params) {
return PropertiesHelper.getInt( INCREMENT_PARAM, params, DEFAULT_INCREMENT_SIZE );
}
|
protected int determineInitialValue(Properties params) {
return PropertiesHelper.getInt( INITIAL_PARAM, params, DEFAULT_INITIAL_VALUE );
}
|
protected String determineSegmentColumnName(Properties params) {
return PropertiesHelper.getString( SEGMENT_COLUMN_PARAM, params, DEF_SEGMENT_COLUMN );
}
Determine the name of the column used to indicate the segment for each
row. This column acts as the primary key.
Called during configuration . |
protected int determineSegmentColumnSize(Properties params) {
return PropertiesHelper.getInt( SEGMENT_LENGTH_PARAM, params, DEF_SEGMENT_LENGTH );
}
|
protected String determineSegmentValue(Properties params) {
String segmentValue = params.getProperty( SEGMENT_VALUE_PARAM );
if ( StringHelper.isEmpty( segmentValue ) ) {
segmentValue = determineDefaultSegmentValue( params );
}
return segmentValue;
}
Determine the segment value corresponding to this generator instance.
Called during configuration . |
protected String determineValueColumnName(Properties params) {
return PropertiesHelper.getString( VALUE_COLUMN_PARAM, params, DEF_VALUE_COLUMN );
}
Determine the name of the column in which we will store the generator persistent value.
Called during configuration . |
protected String determneGeneratorTableName(Properties params) {
String name = PropertiesHelper.getString( TABLE_PARAM, params, DEF_TABLE );
boolean isGivenNameUnqualified = name.indexOf( '." ) < 0;
if ( isGivenNameUnqualified ) {
// if the given name is un-qualified we may neen to qualify it
String schemaName = params.getProperty( SCHEMA );
String catalogName = params.getProperty( CATALOG );
name = Table.qualify( catalogName, schemaName, name );
}
return name;
}
Determine the table name to use for the generator values.
Called during configuration . |
public Serializable doWorkInCurrentTransaction(Connection conn,
String sql) throws SQLException {
int result;
int rows;
do {
SQL_STATEMENT_LOGGER.logStatement( selectQuery, FormatStyle.BASIC );
PreparedStatement selectPS = conn.prepareStatement( selectQuery );
try {
selectPS.setString( 1, segmentValue );
ResultSet selectRS = selectPS.executeQuery();
if ( !selectRS.next() ) {
PreparedStatement insertPS = null;
try {
result = initialValue;
SQL_STATEMENT_LOGGER.logStatement( insertQuery, FormatStyle.BASIC );
insertPS = conn.prepareStatement( insertQuery );
insertPS.setString( 1, segmentValue );
insertPS.setLong( 2, result );
insertPS.execute();
}
finally {
if ( insertPS != null ) {
insertPS.close();
}
}
}
else {
result = selectRS.getInt( 1 );
}
selectRS.close();
}
catch ( SQLException sqle ) {
log.error( "could not read or init a hi value", sqle );
throw sqle;
}
finally {
selectPS.close();
}
SQL_STATEMENT_LOGGER.logStatement( updateQuery, FormatStyle.BASIC );
PreparedStatement updatePS = conn.prepareStatement( updateQuery );
try {
long newValue = optimizer.applyIncrementSizeToSourceValues()
? result + incrementSize : result + 1;
updatePS.setLong( 1, newValue );
updatePS.setLong( 2, result );
updatePS.setString( 3, segmentValue );
rows = updatePS.executeUpdate();
}
catch ( SQLException sqle ) {
log.error( "could not updateQuery hi value in: " + tableName, sqle );
throw sqle;
}
finally {
updatePS.close();
}
}
while ( rows == 0 );
accessCount++;
return new Integer( result );
}
|
public synchronized Serializable generate(SessionImplementor session,
Object obj) {
return optimizer.generate(
new AccessCallback() {
public long getNextValue() {
return ( ( Number ) doWorkInNewTransaction( session ) ).longValue();
}
}
);
}
|
public Object generatorKey() {
return tableName;
}
|
public final Type getIdentifierType() {
return identifierType;
}
Type mapping for the identifier. |
public final int getIncrementSize() {
return incrementSize;
}
The amount of increment to use. The exact implications of this
depends on the optimizer being used. |
public final int getInitialValue() {
return initialValue;
}
The initial value to use when we find no previous state in the
generator table corresponding to our sequence. |
public final Optimizer getOptimizer() {
return optimizer;
}
The optimizer being used by this generator. |
public final String getSegmentColumnName() {
return segmentColumnName;
}
The name of the column in which we store the segment to which each row
belongs. The value here acts as PK. |
public final String getSegmentValue() {
return segmentValue;
}
The value in segment column which
corresponding to this generator instance. In other words this value
indicates the row in which this generator instance will store values. |
public final int getSegmentValueLength() {
return segmentValueLength;
}
The size of the segment column in the
underlying table.
NOTE : should really have been called 'segmentColumnLength' or
even better 'segmentColumnSize' |
public final long getTableAccessCount() {
return accessCount;
}
Getter for property 'tableAccessCount'. Only really useful for unit test
assertions. |
public final String getTableName() {
return tableName;
}
The name of the table in which we store this generator's persistent state. |
public final String getValueColumnName() {
return valueColumnName;
}
The name of the column in which we store our persistent generator value. |
public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
return new String[] {
new StringBuffer()
.append( dialect.getCreateTableString() )
.append( ' " )
.append( tableName )
.append( " ( " )
.append( segmentColumnName )
.append( ' " )
.append( dialect.getTypeName( Types.VARCHAR, segmentValueLength, 0, 0 ) )
.append( ", " )
.append( valueColumnName )
.append( ' " )
.append( dialect.getTypeName( Types.BIGINT ) )
.append( ", primary key ( " )
.append( segmentColumnName )
.append( " ) ) " )
.toString()
};
}
|
public String[] sqlDropStrings(Dialect dialect) throws HibernateException {
StringBuffer sqlDropString = new StringBuffer().append( "drop table " );
if ( dialect.supportsIfExistsBeforeTableName() ) {
sqlDropString.append( "if exists " );
}
sqlDropString.append( tableName ).append( dialect.getCascadeConstraintsString() );
if ( dialect.supportsIfExistsAfterTableName() ) {
sqlDropString.append( " if exists" );
}
return new String[] { sqlDropString.toString() };
}
|