simplify code examples by using conveniences of HibernatePersistenceConfiguration

This commit is contained in:
Gavin King 2024-11-24 10:20:14 +01:00
parent 438b9bdc85
commit 030e1fdc25
2 changed files with 11 additions and 10 deletions

View File

@ -239,8 +239,14 @@ SessionFactory sessionFactory =
new HibernatePersistenceConfiguration("Bookshop")
.managedClass(Book.class)
.managedClass(Author.class)
// Set properties
...
// PostgreSQL
.jdbcUrl("jdbc:postgresql://localhost/example")
// Credentials
.jdbcCredentials(user, password)
// Automatic schema export
.schemaToolingAction(Action.SPEC_ACTION_DROP_AND_CREATE)
// SQL statement logging
.showSql(true, true, true)
// Create a new SessionFactory
.createEntityManagerFactory();
----

View File

@ -272,8 +272,6 @@ package org.hibernate.example;
import org.hibernate.jpa.HibernatePersistenceConfiguration;
import static java.lang.System.out;
import static jakarta.persistence.PersistenceConfiguration.*;
import static org.hibernate.cfg.JdbcSettings.*;
public class Main {
public static void main(String[] args) {
@ -281,15 +279,12 @@ public class Main {
new HibernatePersistenceConfiguration("Bookshelf")
.managedClass(Book.class)
// use H2 in-memory database
.property(JDBC_URL, "jdbc:h2:mem:db1")
.property(JDBC_USER, "sa")
.property(JDBC_PASSWORD, "")
.jdbcUrl("jdbc:h2:mem:db1")
.jdbcCredentials("sa", "")
// use Agroal connection pool
.property("hibernate.agroal.maxSize", 20)
// display SQL in console
.property(SHOW_SQL, true)
.property(FORMAT_SQL, true)
.property(HIGHLIGHT_SQL, true)
.showSql(true, true, true)
.createEntityManagerFactory();
// export the inferred database schema