Hi zusammen.
Also danke für den Tip.
Ich habe mir mal die Mühe gemacht und eine einfache Implementation erstellt.
Ich poste mal den Quellcode, es könnte ja sein das Vieleicht noch andere sowas brauchen.
Code:
package de.meinpackage;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.IdentifierGeneratorFactory;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.mapping.Table;
import org.hibernate.type.Type;
import org.hibernate.util.StringHelper;
public class ParamIncrementGenerator implements IdentifierGenerator, Configurable {
private static final Log log = LogFactory.getLog(ParamIncrementGenerator.class);
private long next;
private String sql;
private Class returnClass;
private long startValue;
public synchronized Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
if (sql!=null) {
getNext( session );
}
return IdentifierGeneratorFactory.createNumber(next++, returnClass);
}
public void configure(Type type, Properties params, Dialect d)
throws MappingException {
String startValueStr= params.getProperty("startvalue");
if (startValueStr == null)startValueStr="1";
startValue = Long.parseLong(startValueStr);
String tableList = params.getProperty("tables");
if (tableList==null) tableList = params.getProperty(PersistentIdentifierGenerator.TABLES);
String[] tables = StringHelper.split(", ", tableList);
String column = params.getProperty("column");
if (column==null) column = params.getProperty(PersistentIdentifierGenerator.PK);
String schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA);
String catalog = params.getProperty(PersistentIdentifierGenerator.CATALOG);
returnClass = type.getReturnedClass();
StringBuffer buf = new StringBuffer();
for ( int i=0; i<tables.length; i++ ) {
if (tables.length>1) {
buf.append("select ").append(column).append(" from ");
}
buf.append( Table.qualify(catalog, schema, tables[i], d.getSchemaSeparator() ) );
if ( i<tables.length-1) buf.append(" union ");
}
if (tables.length>1) {
buf.insert(0, "( ").append(" ) ids_");
column = "ids_." + column;
}
sql = "select max(" + column + ") from " + buf.toString();
}
private void getNext( SessionImplementor session ) {
Connection conn = session.connection();
log.debug("fetching initial value: " + sql);
try {
PersistentIdentifierGenerator.SQL.debug(sql);
PreparedStatement st = conn.prepareStatement(sql);
ResultSet rs = null;
try {
rs = st.executeQuery();
if ( rs.next() ) {
next = rs.getLong(1) + 1;
if ( rs.wasNull() ) next = startValue + 1;
}
else {
next = startValue + 1;
}
sql=null;
log.debug("first free id: " + next);
}
finally {
if (rs!=null) rs.close();
st.close();
}
}
catch (SQLException sqle) {
throw JDBCExceptionHelper.convert(
session.getFactory().getSQLExceptionConverter(),
sqle,
"could not fetch initial value",
sql
);
}
}
}
Hier der Ausschnitt für die hbm Datei.
Code:
<id name="customerID" column="customerID" type="int" unsaved-value="null">
<generator class="de.meinpackage.ParamIncrementGenerator">
<param name="startvalue">1000</param>
</generator>
</id>
Gruss
Ertan