【问题标题】:hibernate annotation exception : Unable to create unique key constraint on table休眠注释异常:无法在表上创建唯一键约束
【发布时间】:2019-08-26 10:52:50
【问题描述】:

我的hibernate配置如下:

@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 时出错:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException: Unable to create unique key constraint (number, person_type ,company_id) 对表persons:找不到数据库列'number, person_type,company_id'。确保使用正确的列名,这取决于所使用的命名策略(它可能与实体中的属性名称不同,尤其是关系类型)

【问题讨论】:

    标签: hibernate annotations spring-data-jpa


    【解决方案1】:

    而不是

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

    使用

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

    String[] 提供列名

    【讨论】:

    • 感谢 Dirk Deyne。根据休眠文档“确保为@UniqueConstraint 的columnNames 属性使用数据库级别的列名。例如,对于简单类型,数据库级别的列名可能与实体级别的属性名相同,这关系属性通常不是这种情况。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    相关资源
    最近更新 更多