title: Hibernate Java configuration spring boot
body: <p>Hi I am trying to make Java configuration for hibernate from application.properties but the code execution gives the following error.
  I am getting mapping error but I checked everything my mapping is correct.It says an id is not while creating <strong><em>sessionFactory</em></strong> bean.
  I have tried to correct but I am not able to please help.</p>
  
  <p>Here is my DBConfig file:</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @PropertySource(value = {"classpath:application.properties"})
  public class DBConfig {
  
  @Value("${spring.datasource.driver-class-name}")
  public String driver;
  
  @Value("${spring.datasource.url}")
  public String url;
  
  @Value("${spring.datasource.username}")
  public String username;
  
  @Value("${spring.datasource.password}")
  public String password;
  
  @Value("${spring.jpa.properties.hibernate.dialect}")
  public String dialect;
  
  @Value("${spring.jpa.hibernate.ddl-auto}")
  public String ddl;
  
  @Bean(name = "dataSource")
  public DriverManagerDataSource dataSource() {
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName(driver);
      dataSource.setUrl(url);
      dataSource.setUsername(username);
      dataSource.setPassword(password);
  
      return dataSource;
  }
  
  
  @Bean(name = "sessionFactory")
  public LocalSessionFactoryBean hibernateSessionFactory() {
      LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
      sessionFactory.setDataSource(dataSource());
      sessionFactory.setPackagesToScan(new String[] { "com.abc.model" });
      sessionFactory.setHibernateProperties(hibernateProperties());
      return sessionFactory;
  }
  
  //Hibernate Transaction
  @Bean 
  HibernateTransactionManager transactionManager() {
      HibernateTransactionManager transactionManager = new HibernateTransactionManager();
      transactionManager.setSessionFactory((SessionFactory) hibernateSessionFactory());
      return transactionManager;
  }
  
  
  
  @Bean
  public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
     return new PersistenceExceptionTranslationPostProcessor();
  }
  
  Properties hibernateProperties() {
      return new Properties() {
         {
            setProperty("hibernate.hbm2ddl.auto",ddl);
            setProperty("hibernate.connection.useUnicode","true");
            setProperty("spring.jpa.hibernate.ddl-auto",ddl);
            setProperty("hibernate.dialect", dialect);
            setProperty("spring.jpa.properties.hibernate.dialect",dialect);
            setProperty("hibernate.globally_quoted_identifiers", "true");
            setProperty("hibernate.connection.CharSet", "utf8mb4");
            setProperty("hibernate.connection.characterEncoding", "utf8");
  
         }
      };
   }
  }
  </code></pre>
  
  <p>When I try to run this I get this error:</p>
  
  <pre><code>Caused by: org.hibernate.MappingException: component property not found: id
  at org.hibernate.mapping.Component.getProperty(Component.java:335) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.cfg.AnnotationBinder.fillComponent(AnnotationBinder.java:2779) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.cfg.AnnotationBinder.fillComponent(AnnotationBinder.java:2620) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.cfg.AnnotationBinder.bindComponent(AnnotationBinder.java:2568) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2197) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:972) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:799) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:250) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:231) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:274) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:84) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:474) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
  at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:511) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
  at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:495) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
  ... 32 common frames omitted
  </code></pre>