1

I have an application (Spring 4 MVC+Hibernate 4+MySQL+Maven integration example using annotations) , integrating Spring with Hibernate using annotation based configuration, I see that the schema is automatically created when I start the app. but I don't have this property hibernate.hbm2ddl.auto in the config !

  @Configuration
    @EnableTransactionManagement
    @ComponentScan({ "fr.telecom.configuration" })
    @PropertySource(value = { "classpath:application.properties" })
    public class HibernateConfiguration {

        @Autowired
        private Environment environment;

        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
           LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
           em.setDataSource(dataSource());
           em.setPackagesToScan(new String[] { "fr.telecom.model" });

           JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
           em.setJpaVendorAdapter(vendorAdapter);
           em.setJpaProperties(hibernateProperties());

           return em;
        }


        @Bean
        public LocalSessionFactoryBean sessionFactory() {
            LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
            sessionFactory.setDataSource(dataSource());
            sessionFactory.setPackagesToScan(new String[] { "fr.telecom.model" });
            sessionFactory.setHibernateProperties(hibernateProperties());
            return sessionFactory;
         }

        @Bean
        public DataSource dataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
            dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
            dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
            dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
            return dataSource;
        }

        private Properties hibernateProperties() {
            Properties properties = new Properties();
            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"));
            return properties;        
        }

        @Bean
        @Autowired
        public HibernateTransactionManager transactionManager(SessionFactory s) {
           HibernateTransactionManager txManager = new HibernateTransactionManager();
           txManager.setSessionFactory(s);
           return txManager;
        }
    }

the table created

CREATE TABLE `t_device` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `device_key` varchar(50) DEFAULT NULL,
  `device_type` varchar(50) DEFAULT NULL,
  `device_desc` varchar(100) DEFAULT NULL,
  `application_id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UK_l3xyqw1dscspkcf3xhn4xiw8` (`device_key`),
  KEY `application_id` (`application_id`),
  CONSTRAINT `FK_pgmvl7toup3p7yem734512uf` FOREIGN KEY (`application_id`) REFERENCES `t_application` (`id`),
  CONSTRAINT `t_device_ibfk_1` FOREIGN KEY (`application_id`) REFERENCES `t_application` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301

1 Answers1

0

Look at this answer.

It says that the default behaviour of hibernate.hbm2ddl.auto automatically validates or exports schema DDL to the database when the SessionFactory is created.

Community
  • 1
  • 1
Pavoletto
  • 140
  • 10
  • how then is possible to avoid automatically validates or exports schema DDL to the database when the SessionFactory is created. – Nuñito Calzada Dec 16 '15 at 17:30
  • Sorry, my mistake, if you read that answer, all the way, it says that if you delete the property the schema isn't automatically created. Anyway, can you post your hibernate configuration? – Pavoletto Dec 16 '15 at 17:35