Atul
Atul

Reputation: 133

Oracle 12c Identity Column with EntityManager

I have created sample table with identity column in 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" ;

I have created the EntityManagerConfiguration using below configuration

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

Then I have created the BookRepository:

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

Now when I am trying to persist the Book to my database I am facing issue.

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

I think I am facing the above issue because of the identity column(oracle 12c feature).

Can someone suggest what I need to fix/add to run the code?

Upvotes: 0

Views: 677

Answers (1)

amicoderozer
amicoderozer

Reputation: 2144

It seems that you have not defined a primary key in your Book entity. Try to add the @Id annotation to the primary key of the entity. Take a look at this link for the solution:

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

Upvotes: 1

Related Questions