Consultor Eletrônico



Kbase P125965: How to generate ids using a sequence on Hibernate
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   3/10/2009
Status: Unverified

GOAL:

How to generate ids using a sequence on Hibernate

GOAL:

How does Hibernate generate ids for Progress Database

GOAL:

How to customize Hibernate to generate ids using Progress Sequences

FACT(s) (Environment):

OpenEdge 10.1x
All Supported Operating Systems

FIX:

Hibernate uses for each target database a class that specifies some specific behaviors. The class provided for Progress Database still based on the capabilities of version 9.1C/D, and lacks lots of the functionalities Progress provides in version 10. The following class is an updated version of the original ProgressDialect provided with Hibernate 3.2, and added two features:
1 - Generates the foreign key for child entities
2 -Generates the ids using sequences (notice that is not a sequence per table, but one sequence for the entire database).
//$Id: ProgressDialect.java 4609 2004-09-27 03:17:46Z oneovthafew $
// contributed by Phillip Baird
// Updated by CPACHECO - PROGRESS SOFTWARE
package org.hibernate.dialect;
import java.sql.Types;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.util.StringHelper;
/**
* An SQL dialect compatible with Progress 9.1C<br>
* <br>
* Connection Parameters required:
* <ul>
* <li>hibernate.dialect org.hibernate.sql.ProgressDialect
* <li>hibernate.driver com.progress.sql.jdbc.JdbcProgressDriver
* <li>hibernate.url jdbc:JdbcProgress:T:host:port:dbname;WorkArounds=536870912
* <li>hibernate.username username
* <li>hibernate.password password
* </ul>
* The WorkArounds parameter in the URL is required to avoid an error in the
* Progress 9.1C JDBC driver related to PreparedStatements.
*
* @author Phillip Baird
*
*/
public class ProgressDialectUpdated extends Dialect {
public ProgressDialectUpdated() {
super();
registerColumnType(Types.BIT, "bit");
registerColumnType(Types.BIGINT, "numeric");
registerColumnType(Types.SMALLINT, "smallint");
registerColumnType(Types.TINYINT, "tinyint");
registerColumnType(Types.INTEGER, "integer");
registerColumnType(Types.CHAR, "character(1)");
registerColumnType(Types.VARCHAR, "varchar($l)");
registerColumnType(Types.FLOAT, "real");
registerColumnType(Types.DOUBLE, "double precision");
registerColumnType(Types.DATE, "date");
registerColumnType(Types.TIME, "time");
registerColumnType(Types.TIMESTAMP, "timestamp");
registerColumnType(Types.VARBINARY, "varbinary($l)");
registerColumnType(Types.NUMERIC, "numeric($p,$s)");
registerColumnType(Types.BLOB, "Blob");
}
public boolean hasAlterTable() {
return true;
}
public String getAddColumnString() {
return "add column";
}
public boolean qualifyIndexName() {
return false;
}
public String getAddForeignKeyConstraintString(String constraintName,
String[] foreignKey, String referencedTable, String[] primaryKey,
boolean referencesPrimaryKey) {
String cols = StringHelper.join(", ", foreignKey);
return new StringBuffer(30).append(" add constraint ").append(
constraintName).append(" foreign key (").append(cols).append(
") references ").append(referencedTable).append(" (").append(
StringHelper.join(", ", primaryKey)).append(')').toString();
}
public boolean supportsSequences() {
return true;
}
public String getSelectSequenceNextValString(String sequenceName) throws MappingException {
// TODO Auto-generated method stub
return sequenceName + ".nextval";
}
public String getSequenceNextVal.String(String sequenceName) throws MappingException {
// TODO Auto-generated method stub
return "select " + getSelectSequenceNextValString( sequenceName ) + " from sysprogress.syssequences where syssequences.\"SEQ-NAME\" ='HIBERNATE_SEQUENCE'";
}

}

Notice that and sequence must be called HIBERNATE_SEQUENCE and be on the same schema as the other tables.
Refers to Hibernate documentation to more details on configuration and usage of it..