1

Why is persistence.xml file needed:

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages="es.japanathome")
public class DataAccessConfig
{

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf)
{
    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(emf);
    return txManager;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
{
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(Boolean.TRUE);
    vendorAdapter.setShowSql(Boolean.TRUE);

    factory.setJpaVendorAdapter( vendorAdapter );
    factory.setDataSource( dataSource() );
    factory.setPackagesToScan("es.japanathome.domain");
    factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
    return factory;
}

Spring knows where can find my entities, so I don't understand why is still needed this file.

1
  • persistence.xml is used to define the persistence-unit and also the classes that will be used by this persistence. Commented May 27, 2014 at 19:34

1 Answer 1

3

Depending on which Spring version you use, you may not need to provide a persistence.xml.

Since Spring 3.1, persistence.xml is no longer explicitly required when you're using LocalContainerEntityManagerFactoryBean. Basically packagesToScan was added allowing a way for Spring to find @Entity classes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.