I am facing below issue with Spring JPA. I have done java configuration with NO xml.
I am getting "java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}"
Below is my application config class.
package co.in.desertlamp.configuration;
import java.util.Properties;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "co.in.desertlamp")
@ComponentScan({ "co.in.desertlamp" })
@PropertySource(value = { "classpath:/co/in/desertlamp/resources/desertlamp.properties" })
public class DesertLampApplicationConfig {
private static final String DATABASE_DRIVER = "db.driver";
private static final String DATABASE_URL = "db.url";
private static final String DATABASE_USERNAME = "db.username";
private static final String DATABASE_PASSWORD = "db.password";
private static final String HIBERNATE_DIALECT = "hibernate.dialect";
private static final String HIBERNATE_SHOWSQL = "hibernate.show_sql";
private static final String HIBERNATE_FORMATSQL = "hibernate.format_sql";
private static final String HIBERNATE_HBM2DDL = "hibernate.hbm2ddl.auto";
@Resource
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "co.in.desertlamp" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty(DATABASE_DRIVER));
dataSource.setUrl(environment.getRequiredProperty(DATABASE_URL));
dataSource.setUsername(environment.getRequiredProperty(DATABASE_USERNAME));
dataSource.setPassword(environment.getRequiredProperty(DATABASE_PASSWORD));
return dataSource;
}
@Bean
@Conditional(DataSourceCondition.class)
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
//Put Logger here
resourceDatabasePopulator.addScript(new ClassPathResource("/co/in/desertlamp/scripts/loadUserAndRoleData.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(dataSource);
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}
private Properties hibernateProperties() {
Properties hibernateProperties = new Properties();
hibernateProperties.put(HIBERNATE_DIALECT, environment.getRequiredProperty(HIBERNATE_DIALECT));
hibernateProperties.put(HIBERNATE_SHOWSQL, environment.getRequiredProperty(HIBERNATE_SHOWSQL));
hibernateProperties.put(HIBERNATE_FORMATSQL, environment.getRequiredProperty(HIBERNATE_FORMATSQL));
hibernateProperties.put(HIBERNATE_HBM2DDL, environment.getRequiredProperty(HIBERNATE_HBM2DDL));
return hibernateProperties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
hibernateTransactionManager.setSessionFactory(sessionFactory);
return hibernateTransactionManager;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(dataSource());
return localContainerEntityManagerFactoryBean;
}
}
Thanks in advance.
Your LocalContainerEntityManagerFactoryBean
needs to know the location of your @Entity
classes.
So adding below properties should fix it:
localContainerEntityManagerFactoryBean.setPackagesToScan("path.to.your.entities");
localContainerEntityManagerFactoryBean.setPersistenceUnitName("name");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With