Home > OS >  Getting "java.lang.reflect.InaccessibleObjectException: Unable to make field private int (varia
Getting "java.lang.reflect.InaccessibleObjectException: Unable to make field private int (varia

Time:07-03

I just learned about JPA and was trying to test it out. And I can't get it to work at all... I get following error messages

7�� 02, 2022 10:09:51 ���� org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [name: c57leePersistence]
7�� 02, 2022 10:09:51 ���� org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.1.0.Final
7�� 02, 2022 10:09:51 ���� org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using built-in connection pool (not intended for production use)
7�� 02, 2022 10:09:51 ���� org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: Loaded JDBC driver class: com.mysql.cj.jdbc.Driver
7�� 02, 2022 10:09:51 ���� org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001012: Connecting with JDBC URL [jdbc:mysql://localhost:3306/POSSystemDemo]
7�� 02, 2022 10:09:51 ���� org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, user=root}
7�� 02, 2022 10:09:51 ���� org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
7�� 02, 2022 10:09:51 ���� org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH10001115: Connection pool size: 20 (min=1)
7�� 02, 2022 10:09:52 ���� org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl logSelectedDialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
7�� 02, 2022 10:09:52 ���� org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PoolState stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/POSSystemDemo]
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: c57leePersistence] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1509)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1430)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:55)
    at [email protected]/jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:80)
    at [email protected]/jakarta.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at com.c57lee.possystemdemo/com.c57lee.possystemdemo.HelloApplication.start(HelloApplication.java:29)
    at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics@18/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics@18/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@18/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Caused by: org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.SingleTableEntityPersister
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:103)
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:75)
    at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.processBootEntities(MappingMetamodelImpl.java:278)
    at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.finishInitialization(MappingMetamodelImpl.java:211)
    at org.hibernate.metamodel.internal.RuntimeMetamodelsImpl.finishInitialization(RuntimeMetamodelsImpl.java:60)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:308)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:415)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1425)
    ... 13 more
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private int com.c57lee.possystemdemo.obj.Test.testId accessible: module com.c57lee.possystemdemo does not "opens com.c57lee.possystemdemo.obj" to unnamed module @191c1c44
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:180)
    at java.base/java.lang.reflect.Field.setAccessible(Field.java:174)
    at org.hibernate.internal.util.ReflectHelper.ensureAccessibility(ReflectHelper.java:412)
    at org.hibernate.internal.util.ReflectHelper.findField(ReflectHelper.java:402)
    at org.hibernate.property.access.internal.PropertyAccessFieldImpl.<init>(PropertyAccessFieldImpl.java:34)
    at org.hibernate.property.access.internal.PropertyAccessStrategyFieldImpl.buildPropertyAccess(PropertyAccessStrategyFieldImpl.java:26)
    at org.hibernate.metamodel.internal.EntityRepresentationStrategyPojoStandard.makePropertyAccess(EntityRepresentationStrategyPojoStandard.java:381)
    at org.hibernate.metamodel.internal.EntityRepresentationStrategyPojoStandard.<init>(EntityRepresentationStrategyPojoStandard.java:146)
    at org.hibernate.metamodel.internal.ManagedTypeRepresentationResolverStandard.resolveStrategy(ManagedTypeRepresentationResolverStandard.java:60)
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:725)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:152)
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:92)
    ... 20 more
Exception running application com.c57lee.possystemdemo.HelloApplication

This is my main method. I made javafx project.

@Override
    public void start(Stage stage) throws IOException {
        
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("c57leePersistence");
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();

    Test test1 = new Test();
    test1.setTestId(1);
    test1.setTestName("testname");
    test1.setTestScore(70);
    System.out.println(test1);
    em.persist(test1);
    em.getTransaction().commit();
    }

This is my test class. I just made it to see if JPA is working.

package com.c57lee.possystemdemo.obj;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Test {
    @Id
    private int testId;
    private String testName;
    private int testScore;

    public Test() {
    }

    public Test(int testId, String testName, int testScore) {
        this.testId = testId;
        this.testName = testName;
        this.testScore = testScore;
    }

    public int getTestId() {
        return testId;
    }

    public void setTestId(int testId) {
        this.testId = testId;
    }

    public String getTestName() {
        return testName;
    }

    public void setTestName(String testName) {
        this.testName = testName;
    }

    public int getTestScore() {
        return testScore;
    }

    public void setTestScore(int testScore) {
        this.testScore = testScore;
    }

    @Override
    public String toString() {
        return "Test{"  
                "testId="   testId  
                ", testName='"   testName   '\''  
                ", testScore="   testScore  
                '}';
    }
}

And this is my module-info.java file. I don't know if this is related but this file has 'opens com.c57lee.possystem to ' in it so..

module com.c57lee.possystemdemo {
    requires javafx.controls;
    requires javafx.fxml;
    requires java.sql;
    requires jakarta.persistence;


    opens com.c57lee.possystemdemo to javafx.fxml;
    exports com.c57lee.possystemdemo;
}

And this is my persistence.xml file

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="c57leePersistence"  transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
            <property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/POSSystemDemo" />
            <property name="jakarta.persistence.jdbc.user" value="root" />
            <property name="jakarta.persistence.jdbc.password" value="" />
        </properties>
    </persistence-unit>
</persistence>

Am I missing something?

CodePudding user response:

Based solely on the error:

Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private int com.c57lee.possystemdemo.obj.Test.testId accessible: module com.c57lee.possystemdemo does not "opens com.c57lee.possystemdemo.obj" to unnamed module @191c1c44

Adding opens opens com.c57lee.possystemdemo.obj; to your module descriptor should fix this specific problem. For example:

module com.c57lee.possystemdemo {
    requires javafx.controls;
    requires javafx.fxml;
    requires java.sql;
    requires jakarta.persistence;


    opens com.c57lee.possystemdemo to javafx.fxml;
    exports com.c57lee.possystemdemo;

    opens opens com.c57lee.possystemdemo.obj; // the new opens directive
}

You should do something similar if afterwards you get the same type of error, but for different packages (read the error message to determine which package needs to be opened or exported).

However, given how much JPA/Hibernate relies on reflection, it may be beneficial to simply "open" the entire module. For example:

// notice the new "open" keyword here
open module com.c57lee.possystemdemo {
    requires javafx.controls;
    requires javafx.fxml;
    requires java.sql;
    requires jakarta.persistence;

    exports com.c57lee.possystemdemo;

    // "open" modules cannot have "opens" directives
}

A third option is to simply make your code non-modular (don't include a module-info.java file for your own code). With this option you may need to use the --add-modules JVM argument, particularly for the JavaFX modules (see the Getting Started with JavaFX 11 documentation linked below).


I also recommend reading the following:

  • Related