Search code examples
javahibernatejpa

Unable to build Hibernate SessionFactory; embedded HSQLDB


I would really appreciate if someone could help me with that.

I am using the out of the box jfx example in IntelliJ and tried to implement the JPA/Hibernate Framework with an embedded HSQLDB.

persistence.xml

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence version="3.0" xmlns="https://jakarta.ee/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence
             https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd">


    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
        <description>Hibernate Entity Manager Example</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <class>com.example.test.model.Student</class>
        <class>com.example.test.model.Person</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>

        <properties>
            <property name="jakarta.persistence.jdbc.driver" value="org.hsqldb.jdbc.JDBCDriver"/>
            <property name="jakarta.persistence.jdbc.url" value="jdbc:hsqldb:file:myDB;shutdown=true"/>
            <property name="jakarta.persistence.jdbc.user" value="SA"/>
            <property name="jakarta.persistence.jdbc.password" value=""/>
            <property name="jakarta.persistence.schema-generation.database.action" value="create"/>

pom.xml

<dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.7.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>6.2.4.Final</version>
    </dependency>

Person.class

    @Entity
    public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column
    private String name;
    @Column
    private String firstName;

    public Student() {
    }

The following code gives me an exception:

        Person p = new Person();
        p.setFirstName("Dalia");
        
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
        EntityManager entityManager = entityManagerFactory.createEntityManager();

Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory ... Could not instantiate persister org.hibernate.persister.entity.SingleTableEntityPersister ... Unable to make field private java.lang.Long com.example.test.model.Student.id accessible: module com.example.test does not "opens com.example.test.model" to unnamed module @39181562

Thanks a lot!


Solution

  • This issue is related to the Java module packaging. Referenced libraries have to be mentioned in your module-info.java.