Hibernate Error accessing field [private java.lang.String com.example.demo.Student.firstName] by reflection for persistent property

Hibernate Error accessing field [private java.lang.String com.example.demo.Student.firstName] by reflection for persistent property

Questions : Hibernate Error accessing field [private java.lang.String com.example.demo.Student.firstName] by reflection for persistent property


I have a very simple Hibernate set up, but I in4codes_hibernate receive this strange error when saving an in4codes_hibernate entity:

org.hibernate.property.access.spi.PropertyAccessException: in4codes_hibernate Error accessing field [private in4codes_hibernate java.lang.String in4codes_hibernate com.example.demo.Student.firstName] by in4codes_hibernate reflection for persistent property in4codes_hibernate [com.example.demo.Student#firstName] : in4codes_hibernate Student [id=0, firstName=My, lastName=Name]

This is how I save it:

StudentDao studentDao = new StudentDao(); Student student = new Student("My", "Name"); studentDao.saveStudent(student); 

This is my Student entity:

package com.example.demo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "student") public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private int id; @Column(name = "first_name") private String firstName; @Column(name = "last_name") private String lastName; public Student() { } public Student(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]"; } } 


package com.example.demo; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import com.example.demo.Student; import com.example.demo.HibernateUtil; public class StudentDao { public void saveStudent(Student student) { Transaction transaction = null; try (Session session = HibernateUtil.getSessionFactory().openSession()) { // start a transaction transaction = session.beginTransaction(); // save the student object session.save(student); // commit transaction transaction.commit(); } catch (Exception e) { e.printStackTrace(); if (transaction != null) { transaction.rollback(); } } } public List <Student> getStudents() { try (Session session = HibernateUtil.getSessionFactory().openSession()) { return session.createQuery("from Student", Student.class).list(); } } } 


package com.example.demo; import java.util.Properties; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.service.ServiceRegistry; import com.example.demo.Student; public class HibernateUtil { private static SessionFactory sessionFactory; public static SessionFactory getSessionFactory() { if (sessionFactory == null) { try { Configuration configuration = new Configuration(); // Hibernate settings equivalent to hibernate.cfg.xml's properties Properties settings = new Properties(); settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver"); settings.put(Environment.URL, "jdbc:mysql://localhost:3306/hibernate_db?useSSL=false&serverTimezone=UTC"); settings.put(Environment.USER, "root"); settings.put(Environment.PASS, "root"); settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect"); settings.put(Environment.SHOW_SQL, "true"); settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread"); settings.put(Environment.HBM2DDL_AUTO, "create-drop"); configuration.setProperties(settings); configuration.addAnnotatedClass(Student.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) { e.printStackTrace(); } } return sessionFactory; } } 

And my dependencies:

 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.3.7.Final</version> </dependency> 

I have tried different versions of in4codes_hibernate hibernate-core, but without any effect.

Total Answers 1

Answers 1 : of Hibernate Error accessing field [private java.lang.String com.example.demo.Student.firstName] by reflection for persistent property

I’m using spring framework. After in4codes_hibernate removing the following dependency, it in4codes_hibernate worked:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> 


Answer Link
  • Unable to run NoraUI mvn verify goal
  • Unable to run my app on emulator in VS Code
  • Unable to run multiple instances of libVLC(MobileVLCKit) in IOS via flutter framework
  • Unable to run make on griddb source on ubuntu 20.04 (building from source)
  • Unable to run latexindent macOS Monterey 12.0.1
  • Unable to run kotlinc-native command
  • Unable to run JUnit Test… Java.lang.ExceptionInInitializerError (Android Studio)
  • Unable to run java with -Xmx > 966m
  • Unable to run ionic cap run android from wsl2 inorder to start android emulator
  • Unable to run Intel HAXM installer: Cannot start process, the working directory does not exist
  • fs
  • Unable to run Google Analytics sample code
  • unable to run flutter run after upgarding to flutter 2.8.0 from 2.5.3
  • Unable to run Django with PostgreSQL in Docker
  • Unable to Run Container Using testcontainers
  • Unable to run ClojureScript Hello World program, Error building classpath. Error reading edn.
  • unable to run client command for apache karaf 4.3.3 through remote server
  • Unable to run c program 2nd time using eclipse
  • unable to run c++ in visual studio code on m1 chipset
  • Unable to run Android Instrumented Tests
  • Unable to run adb, check your Android SDK installation and ANDROID_SDK_ROOT environment variable: …AndroidSdkplatform-toolsadb.exe
  • Unable to run a singlespecific .spec.ts file through angular cli using ng test –include option
  • Unable to run a Mango query
  • Unable to return response back to view in laravel from package
  • Unable to return object reference in std::optional