[hibernate-dev] api/doc suggestions

Alessio Stalla alessiostalla at gmail.com
Sun Feb 9 17:05:44 EST 2020


On Sun, 9 Feb 2020 at 09:34, Max Rydahl Andersen <manderse at redhat.com>
wrote:

> ### Document simplest programatic configuration
> (This might just be "max is stupid" issue - please tell me :)
>
> Trying to do a simple java main method booting Hibernate I could not
> figure out with the "new" metadata API a simple way to just start
> Hibernate.
>
> What is the equivalent way to do this in new metadata api:
>
> ```
> new Configuration().setProperty("hibernate.dialect",
> "org.hibernate.dialect.H2Dialect")
>                      .setProperty("hibernate.connection.url",
> "jdbc:h2:./sakila")
>                      .setProperty("hibernate.connection.username", "sa")
>                      .buildSessionFactory();
> ```
>
> Just setup dialect, give connection info and get a
> sessionfactory/entitymanager ?
>
> I found apis to start from a file containing those settings; but I
> really just want a programmatic api
> to use in batch script etc. without a need for external files.
>

In Portofino, we do this:
https://github.com/ManyDesigns/Portofino/blob/a411efb42c9f2665e913dd0116ec2d201aff2a3a/portofino-database/src/main/java/com/manydesigns/portofino/persistence/hibernate/SessionFactoryBuilder.java#L158
It boils down to something like:

Map<String, Object> settings = new HashMap<>();
settings.put("hibernate.dialect", "...");
//etc.
ServiceRegistry standardRegistry = new
StandardServiceRegistryBuilder().applySettings(settings).build();
MetadataSources sources = new MetadataSources(standardRegistry);
sources.add{Class|Package|Jar}(...);
//etc.
SessionFactory sessionFactory =
sources.getMetadataBuilder().build().getSessionFactoryBuilder().build();

It's not exactly simple, but it could be easily wrapped in a nice
higher-order static method, I think.


More information about the hibernate-dev mailing list