You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法构建Hibernate SessionFactory:嵌入式HSQLDB权限问题求助

解决JavaFX项目中JPA/Hibernate集成HSQLDB的模块访问异常

问题场景

在IntelliJ默认JavaFX示例项目中集成JPA/Hibernate与嵌入式HSQLDB时,初始化EntityManagerFactory触发权限异常。

相关配置与代码

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"/>
        </properties>
    </persistence-unit>
</persistence>

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>

实体类Student.java(注:测试代码中误写为Person)

@Entity
public class Student {

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

    public Student() {
    }

    // 需补充getter/setter方法
}

测试代码

// 注意:此处应实例化Student而非Person
Student p = new Student();
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

解决方案

异常根源是Java模块系统的反射访问限制:Hibernate需通过反射操作实体类私有字段,但项目启用模块后未授权Hibernate所在的未命名模块访问实体包。提供两种解决方式:

方式1:修改module-info.java

在项目的module-info.java中添加opens声明,开放实体包给未命名模块:

module com.example.test {
    // 保留原有模块依赖声明(如requires javafx.controls等)
    opens com.example.test.model to unnamed module;
}

若需持久化该包下所有实体,也可使用exports,但opens仅开放反射权限,更安全。

方式2:添加JVM启动参数

无需修改模块配置,在运行时添加VM参数强制开放反射权限:

--add-opens com.example.test.model/unnamed module=ALL-UNNAMED

在IntelliJ中,可在Run Configuration的VM options中配置该参数。

额外注意事项

  1. 测试代码中需将new Person()改为new Student(),避免类实例化错误;
  2. 确保persistence.xml放置在src/main/resources/META-INF目录下,保证Hibernate能正确加载配置。

内容的提问来源于stack exchange,提问作者Marko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 12:07:51