My hibernate configuration as follows :
@Bean
public JpaDialect hibernateJpaDialect() {
HibernateJpaDialect hibernateJpaDialect = new HibernateJpaDialect();
hibernateJpaDialect.setPrepareConnection(true);
return hibernateJpaDialect;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(new String[]{BaseEntity.class.getPackage().getName()});
entityManagerFactoryBean.setJpaVendorAdapter(hibernateJpaVendorAdapter(dataSourceConfiguration));
entityManagerFactoryBean.setJpaProperties(hibernateProperties(dataSourceConfiguration));
entityManagerFactoryBean.setJpaDialect(hibernateJpaDialect());
return entityManagerFactoryBean;
}
@Bean
public DataSource dataSource() {
return new HikariDataSource(hikariConfig(dataSourceConfiguration));
}
@Bean
public HibernateJpaVendorAdapter hibernateJpaVendorAdapter(DataSourceConfiguration dataSourceConfiguration) {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
hibernateJpaVendorAdapter.setShowSql(Boolean.valueOf(dataSourceConfiguration.getShowsql()));
hibernateJpaVendorAdapter.setGenerateDdl(Boolean.valueOf(dataSourceConfiguration.getDdlGeneration()));
return hibernateJpaVendorAdapter;
}
@Bean
public EclipseLinkJpaVendorAdapter eclipseLinkJpaVendorAdapter(DataSourceConfiguration dataSourceConfiguration) {
EclipseLinkJpaVendorAdapter eclipseLinkJpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
eclipseLinkJpaVendorAdapter.setDatabase(Database.MYSQL);
eclipseLinkJpaVendorAdapter.setShowSql(Boolean.valueOf(dataSourceConfiguration.getShowsql()));
eclipseLinkJpaVendorAdapter.setGenerateDdl(Boolean.valueOf(dataSourceConfiguration.getDdlGeneration()));
return eclipseLinkJpaVendorAdapter;
}
private HikariConfig hikariConfig(DataSourceConfiguration dataSourceConfiguration) {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(dataSourceConfiguration.getDriver());
hikariConfig.setJdbcUrl(dataSourceConfiguration.getUrl());
hikariConfig.setUsername(dataSourceConfiguration.getUser());
hikariConfig.setPassword(dataSourceConfiguration.getPassword());
return hikariConfig;
}
private Properties hibernateProperties(DataSourceConfiguration dataSourceConfiguration) {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", dataSourceConfiguration.getDdlGeneration());
properties.setProperty("hibernate.dialect", dataSourceConfiguration.getDialect());
properties.setProperty("hibernate.current_session_context_class", dataSourceConfiguration.getCurrentSession());
properties.setProperty("hibernate.show_sql", dataSourceConfiguration.getShowsql());
properties.setProperty("hibernate.format_sql", dataSourceConfiguration.getFormatsql());
properties.setProperty("hibernate.discriminator.ignore_explicit_for_joined", "true");
return properties;
}
Entity :
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Getter
@Setter
@Inheritance(strategy = InheritanceType.JOINED)
//@EqualsAndHashCode(callSuper = true)
@DiscriminatorColumn(name = "person_type")
@Table(name = "persons",
uniqueConstraints = {
@UniqueConstraint(columnNames = {"number, person_type ,company_id"})
},
indexes = {
@Index(name = "person_firstname_index", columnList = "firstname"),
@Index(name = "person_lastname_index", columnList = "lastname"),
@Index(name = "person_first_lastname_index", columnList = "firstname,lastname"),
@Index(name = "person_email_index", columnList = "email"),
@Index(name = "person_number_index", columnList = "number, company_id"),
@Index(name = "person_mobile_index", columnList = "mobile"),
@Index(name = "person_email_mobile_number_index", columnList = "number,email,mobile"),
})
public abstract class Person extends BaseEntity implements ActorProfile {}
Exception : Error creating bean with name 'entityManagerFactory' defined in com.orsbv.hcs.config.HCSRepositoryContext: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to create unique key constraint (number, person_type ,company_id) on table persons: database column 'number, person_type ,company_id' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)
instead of
@UniqueConstraint(columnNames = {"number, person_type ,company_id"})
use
@UniqueConstraint(columnNames = {"number", "person_type" , "company_id" })
String[] to provide the column names