3

I would like to know if it is possible to programmatically provide a DataSource object to hibernate configuration?
In our system we construct a datasource object (this is a Java SE application), and I would like to move from plain JDBC code to hibernate.
If someone knows the answer with JPA this is also fine.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Yair Zaslavsky
  • 4,091
  • 4
  • 20
  • 27
  • I found a question here in SO which might be a duplicate of yours (or might answer your question): http://stackoverflow.com/questions/4406935/how-can-i-set-datasource-when-im-creating-hibernate-sessionfactory – helderdarocha Jun 12 '14 at 02:14
  • Similar: https://stackoverflow.com/q/4406935/642706 & https://stackoverflow.com/q/884154/642706 – Basil Bourque Aug 14 '23 at 08:40
  • I [posted an Answer](https://stackoverflow.com/a/76901981/642706) on a similar Question. There I show source code of a complete example of how to configure Hibernate/Jakarta Persistence in code-only (no XML files, no JNDI) using an existing `DataSource` instance. – Basil Bourque Aug 14 '23 at 22:46

2 Answers2

4

You can use a org.hibernate.cfg.Configuration object.

For example - a datasource:

Configuration cfg = new Configuration()
    .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
    .setProperty("hibernate.connection.datasource", "java:/MySQLDS");

or a driver manager:

Configuration cfg = new Configuration()
    .setProperty("hibernate.connection.driver_class", "org.postgresql.Driver")
    .setProperty("hibernate.connection.url", "jdbc:postgresql://localhost/test")
    .setProperty("hibernate.connection.username", "user")
    .setProperty("hibernate.connection.username", "pass");

See: Hibernate Programmatic Configuration

helderdarocha
  • 23,209
  • 4
  • 50
  • 65
  • 2
    Hi, what if I have a datasource class in my code, and I would like to use it? – Yair Zaslavsky Jun 12 '14 at 02:02
  • You want to configure your own datasource provider? If that's the case I think you will have to implement a [DatasourceConnectionProvider](http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/connection/DatasourceConnectionProvider.html) which contains a method `setDatasource()`. Then you have to set the `hibernate.connection.provider` property in Hibernate passing your provider as an argument. – helderdarocha Jun 12 '14 at 02:13
  • 2
    @YairZaslavsky very late, but it's not necessary to implement a connection provider. See my answer. – Allan Veloso Mar 17 '19 at 02:12
0

Yes, it's possible. You will just need to implement a PersistenceUnitInfo:

public class PersistenceOptions implements PersistenceUnitInfo {

    private DataSource jtaDataSource;

    void setJtaDataSource(DataSource jtaDataSource){
        this.jtaDataSource = jtaDataSource;
    }

    @Override
    public DataSource getNonJtaDataSource() {
        return jtaDataSource;
    }

    // ... You will have to implement a bunch of other methods that are used 
    // by Hibernate configuration, like getProperties()
}

Usage:

PersistenceOptions options = new PersistenceOptions();
DataSource myFancyDataSource = new BasicDataSource();
options.setJtaDataSource(myFancyDataSource);
EntityManagerFactory emf = new EntityManagerFactoryBuilderImpl(
        new PersistenceUnitInfoDescriptor(options), null
).build();
EntityManager em = emf.createEntityManager();
Allan Veloso
  • 5,823
  • 1
  • 38
  • 36