Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity class configuration in a Spring Java configuration

In a Spring Hibernate XML configuration, I have

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    // ...
    <property name="annotatedClasses">
        <list>
            <value>com.abc.xyz.Foo</value>
            // ...
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
        // ....
        </props>
    </property>
    //..
</bean>

I know an equivalent of a Spring Java configuration is the following: LocalSessionFactoryBuilder(dataSource()).addAnnotatedClasses(Foo.class).buildSessionFactory();

My question is how to do the same if I don't use the LocalSessionFactoryBuilder class, but HibernateJpaVendorAdapter and LocalContainerEntityManagerFactoryBean classes?

like image 977
vic Avatar asked Feb 19 '26 08:02

vic


1 Answers

You can do it in following way:

    @Bean
    public AbstractEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setPackagesToScan("com.abc.xyz.Foo");
        entityManagerFactory.setPersistenceProvider(new HibernatePersistence());
        entityManagerFactory.getJpaPropertyMap().put("hibernate.hbm2ddl.auto", "validate");

        return entityManagerFactory;
    }
like image 111
Jakub Kubrynski Avatar answered Feb 21 '26 21:02

Jakub Kubrynski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!