First remove all of your configuration Spring Boot will start it for you.
Make sure you have an application.properties
in your classpath and add the following properties.
spring.datasource.url=jdbc:postgresql://localhost:5432/teste?charSet=LATIN1 spring.datasource.username=klebermo spring.datasource.password=123 spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect spring.jpa.show-sql=false spring.jpa.hibernate.ddl-auto=create
If you really need access to a SessionFactory
and that is basically for the same datasource, then you can do the following (which is also documented here although for XML, not JavaConfig).
@Configuration public class HibernateConfig { @Bean public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) { HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean(); factory.setEntityManagerFactory(emf); return factory; } }
That way you have both an EntityManagerFactory
and a SessionFactory
.
UPDATE: As of Hibernate 5 the SessionFactory
actually extends the EntityManagerFactory
. So to obtain a SessionFactory
you can simply cast the EntityManagerFactory
to it or use the unwrap
method to get one.
public class SomeHibernateRepository { @PersistenceUnit private EntityManagerFactory emf; protected SessionFactory getSessionFactory() { return emf.unwrap(SessionFactory.class); } }
Assuming you have a class with a main
method with @EnableAutoConfiguration
you don’t need the @EnableTransactionManagement
annotation, as that will be enabled by Spring Boot for you. A basic application class in the com.spring.app
package should be enough.
@Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
Something like that should be enough to have all your classes (including entities and Spring Data based repositories) detected.
UPDATE: These annotations can be replaced with a single @SpringBootApplication
in more recent versions of Spring Boot.
@SpringBootApplication public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
I would also suggest removing the commons-dbcp
dependency as that would allow Spring Boot to configure the faster and more robust HikariCP
implementation.