How can I set Datasource when I’m creating Hibernate SessionFactory?

If you happen to have your DataSource stored in JNDI, then simply use: configuration.setProperty( “hibernate.connection.datasource”, “java:comp/env/jdbc/yourDataSource”); But if you use a custom data source provider like Apache DBCP or BoneCP and you don’t want to use a dependency injection framework like Spring, then you may inject it on the StandardServiceRegistryBuilder before creating the SessionFactory: //retrieve … Read more

Spring and hibernate: No Session found for current thread

You annotated your Dao class with @Transactional, but not your service class. The line: Visitor storedVisitor = (Visitor) sessionFactory.getCurrentSession().get(Visitor.class, visitorDetails.getTfscNumber(), LockMode.NONE); requires you to be in a transaction. You can fix this by adding the @Transactional annotation to your ProfileService class, or just the registerVisitor() method.

Hibernate SessionFactory vs. JPA EntityManagerFactory

Prefer EntityManagerFactory and EntityManager. They are defined by the JPA standard. SessionFactory and Session are hibernate-specific. The EntityManager invokes the hibernate session under the hood. And if you need some specific features that are not available in the EntityManager, you can obtain the session by calling: Session session = entityManager.unwrap(Session.class);