Issue
I am using Redshift as a DB for my Java Project with Hibernate support. When the Hibernate starts, it tries to create a sequence, which I do not need.
public class HibernateUtil {
private static SessionFactory SESSION_FACTORY;
private static StandardServiceRegistry registry;
public static synchronized SessionFactory getSessionFactory(DataSource dataSource) {
if (SESSION_FACTORY == null) {
try {
Configuration configuration = new Configuration();
Properties settings = new Properties();
settings.put("show_sql", "false");
settings.put("current_session_context_class", "thread");
settings.put("hbm2ddl.auto", "none");
configuration.setProperties(settings);
registry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.applySetting(Environment.DATASOURCE, dataSource)
.build();
SESSION_FACTORY = configuration.buildSessionFactory(registry);
} catch (Exception e) {
log.error("Error creating Session Factory.", e);
if (registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
}
return SESSION_FACTORY; }
}
This is the stack trace.
ERROR [2019-06-07 14:41:40,743] org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl: Could not fetch the SequenceInformation from the database
! com.amazon.support.exceptions.ErrorException: [Amazon](500310) Invalid operation: relation "information_schema.sequences" does not exist;
! ... 52 common frames omitted
! Causing: java.sql.SQLException: [Amazon](500310) Invalid operation: relation "information_schema.sequences" does not exist;
Solution
I created a custom Dialect that overrode existing PostgreSqlDialect
public class CustomRedshiftDialect extends PostgreSQL81Dialect {
@Override
public String getQuerySequencesString() {
return null;
}
}
And in the HibernateUtil class or the appropriate XML.
// Hibernate settings equivalent to hibernate.cfg.xml's properties
Properties settings = new Properties();
settings.put("show_sql", "false");
settings.put("current_session_context_class", "thread");
settings.put("hbm2ddl.auto", "none");
settings.put("hibernate.dialect", "com.me.vlimbare.factory.db.CustomRedshiftDialect");
Answered By - Vinay Limbare
Answer Checked By - Marie Seifert (JavaFixing Admin)