带有EntityManager的Oracle 12C标识列

时间:2016-07-12 11:27:51

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

我在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> {
}

现在,当我试图将本书保存到我的数据库时,我面临着问题。

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功能),我认为我正面临上述问题。

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

1 个答案:

答案 0 :(得分:1)

您似乎尚未在 Book 实体中定义主键。 尝试将 @Id 注释添加到实体的主键。 请查看此链接以获取解决方案:

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