I have successfully set up a DAO by creating and closing hibernate sessions manually. Here for I used a "hibernate.cfg.xml" file and a "HibernateUtil" class.
I now want to use spring's autowire ability to automatically create and close hibernate sessions.
What I have already done:
(1) Move hibernate.cfg.xml content over to application.properties
spring.datasource.url="dummy"
spring.datasource.username="dummy"
spring.datasource.password="dummy"
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jooq.sql-dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
(2) DOA class
@Repository
public class CarpoolDOA implements ICarpoolDoa {
@Autowired
SessionFactory sessionFactory;
public List<User> getPassengersTour(int tourId) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Tour tour = (Tour) session.get(Tour.class, tourId);
return tour.getPassengers();
}
}
(3) This bean is in place
@Autowired
@Bean(name="sessionFactory")
public SessionFactory sessionFactory(HibernateEntityManagerFactory factory) {
return factory.getSessionFactory();
}
This code actually runs, but it is not using the configuration provided in application.properties. I guess it is loading standard hibernate configurations as seen on startup.
Output on startup:
HHH000412: Hibernate Core {4.3.11.Final}
HHH000206: hibernate.properties not found
HHH000021: Bytecode provider name : javassist
HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
HHH000397: Using ASTQueryTranslatorFactory
HHH000227: Running hbm2ddl schema export
How can I tell Spring Boot to use my configuration?