休眠注释异常:无法在表上创建唯一键约束



我的休眠配置如下:

@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;
        }

实体:

@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 {}

异常:创建在 com.orsbv.hcs.config.HCSRepositoryContext 中定义的名称为"entityManagerFactory"的 Bean 时出错:初始化方法的调用失败;嵌套异常为 org.hibernate.AnnotationException:无法在表 Person 上创建唯一键约束(数字、person_type、company_id(:找不到数据库列"number、person_type、company_id"。确保使用正确的列名称,这取决于所使用的命名策略(它可能与实体中的属性名称不同,尤其是对于关系类型(

而不是

 @UniqueConstraint(columnNames = {"number, person_type ,company_id"})

 @UniqueConstraint(columnNames = {"number", "person_type" , "company_id" })

字符串 [] 提供列名

最新更新