【问题标题】:Oracle 12c Identity Column with EntityManager带有 EntityManager 的 Oracle 12c 身份列
【发布时间】:2016-11-14 14:52:58
【问题描述】:

我在 oracle 中创建了带有标识列的示例表。

CREATE TABLE "CORE_PROD"."BOOK" 
   (    "ID" NUMBER GENERATED BY DEFAULT AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER  NOCYCLE  NOT NULL ENABLE, 
    "AUTHOR" VARCHAR2(255 BYTE), 
    "GENRE" VARCHAR2(255 BYTE), 
    "ISBN" VARCHAR2(255 BYTE), 
    "PUBLISHED" NUMBER NOT NULL ENABLE, 
    "TITLE" VARCHAR2(255 BYTE), 
     PRIMARY KEY ("ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  TABLESPACE "MCA_DATA"  ENABLE
   ) SEGMENT CREATION DEFERRED 

  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
 NOCOMPRESS LOGGING
  TABLESPACE "PROD_DATA" ;

我已经使用以下配置创建了 EntityManagerConfiguration

@Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("oracle.driver.class"));
        dataSource.setUrl(environment.getRequiredProperty("oracle.connection.url"));
        dataSource.setUsername(environment.getRequiredProperty("oracle.db.username"));
        dataSource.setPassword(environment.getRequiredProperty("oracle.db.password"));
        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
        bean.setPackagesToScan(DomainPackage.class.getPackage().getName());
        bean.setDataSource(dataSource());
        bean.setJpaVendorAdapter(jpaVendorAdapter());
        bean.setJpaProperties(jpaProperties());
        return bean;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setShowSql(true);
        jpaVendorAdapter.setGenerateDdl(true);
        return jpaVendorAdapter;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setEntityManagerFactory(emf);
        return jpaTransactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }


    private Properties jpaProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hbm2ddl.auto"));
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        // Configures the naming strategy that is used when Hibernate creates
        // new database objects and schema elements
        // properties.put("hibernate.ejb.naming_strategy",
        // environment.getRequiredProperty("hibernate.ejb.naming_strategy"));
        properties.put("hibernate.temp.use_jdbc_metadata_defaults",
                environment.getRequiredProperty("use_jdbc_metadata_defaults"));
        return properties;
    }

然后我创建了 BookRepository:

public interface BookRepository extends PagingAndSortingRepository<Book, Long> {
}

现在,当我尝试将 Book 持久保存到我的数据库时,我遇到了问题。

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in org.markit.oracle.datasource.config.EntityManagerConfiguration: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: org.markit.oracle.model.Book

我认为由于标识列(oracle 12c 功能)而面临上述问题。

有人可以建议我需要修复/添加什么来运行代码吗?

【问题讨论】:

标签: hibernate jpa spring-data-jpa hibernate-mapping oracle12c


【解决方案1】:

您似乎没有在您的 Book 实体中定义主键。 尝试将 @Id 注释添加到实体的主键。 看看这个链接的解决方案:

http://viralpatel.net/blogs/org-hibernate-annotationexception-no-identifier-specified/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2016-02-15
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    相关资源
    最近更新 更多