I am doing Dynamic Web Project using JPA in Java 17. I have a class called UserEntity. In the first page I have a register button, which has the following functionality:
private void register(HttpServletRequest request, HttpServletResponse response) {
String password = request.getParameter("register-pass");
String repeatPassword = request.getParameter("confirm-register-pass");
if (!password.equals(repeatPassword)) {
request.setAttribute("error", "Password mismatch!");
RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
try {
dispatcher.forward(request, response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
JPAUserRepository repo = new JPAUserRepository();
String username = request.getParameter("register-user");
String email = request.getParameter("register-email");
UserEntity user = new UserEntity(username, password, email);
if(repo.registerUser(user)) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
try {
response.sendRedirect("profile.jsp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
}
}
}
The registerUser function from the repository is:
public boolean registerUser(UserEntity user) {
EntityManager em = getEntityManager();
try {
em.getTransaction().begin();
em.persist(user);
em.getTransaction().commit();
} catch (TransactionalException e) {
em.getTransaction().rollback();
return false;
} finally {
em.close();
}
return true;
}
This is my persistence.xml:
<?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="MetaVerse" transaction-type="RESOURCE_LOCAL">
<provider>
org.hibernate.jpa.HibernatePersistenceProvider
</provider>
<class>uni.fmi.masters.entities.UserEntity</class>
<class>uni.fmi.masters.entities.CommentEntity</class>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.url" value="jdbc:h2:~/MetaVerseDB"/>
<property name="hibernate.connection.user" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.flushMode" value="FLUSH_AUTO"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
When I click on register, NullPointerException error occurs. I have added to the build path javassist-3.27.0-GA-javadoc.jar, but the issue is still not resolved. How can I solve this problem?
Root Cause
java.lang.NullPointerException: Cannot invoke "java.lang.reflect.AccessibleObject.setAccessible(boolean)" because "ao" is null
javassist.util.proxy.SecurityActions.setAccessible(SecurityActions.java:103)
javassist.util.proxy.DefineClassHelper.toClass3(DefineClassHelper.java:151)
javassist.util.proxy.DefineClassHelper.toClass2(DefineClassHelper.java:134)
javassist.util.proxy.DefineClassHelper.toClass(DefineClassHelper.java:95)
javassist.util.proxy.FactoryHelper.toClass(FactoryHelper.java:131)
javassist.util.proxy.ProxyFactory.createClass3(ProxyFactory.java:530)
javassist.util.proxy.ProxyFactory.createClass2(ProxyFactory.java:515)
javassist.util.proxy.ProxyFactory.createClass1(ProxyFactory.java:451)
javassist.util.proxy.ProxyFactory.createClass(ProxyFactory.java:422)
org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.postInstantiate(JavassistProxyFactory.java:75)
org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:162)
org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:156)
org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:58)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:88)
org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:116)
org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:382)
org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:519)
org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:124)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96)
org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77)
org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:129)
org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:300)
org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892)
org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
uni.fmi.masters.repositories.JPAUserRepository.getEntityManager(JPAUserRepository.java:47)
uni.fmi.masters.repositories.JPAUserRepository.registerUser(JPAUserRepository.java:16)
uni.fmi.masters.HelloWorldServlet.register(HelloWorldServlet.java:128)
uni.fmi.masters.HelloWorldServlet.doPost(HelloWorldServlet.java:68)
javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
CodePudding user response:
As far as I know there are multiple things that can cause this
- Your Java POJO Mapping class may not have the empty/default constructor
- Any collections you have should have the default constructor or overridden hashcode and equals method. That is, the same rules apply for any joined collections/parent-children.
If you have your constructors right for POJO classes than check hibernate.properties file and try changing
hibernate.bytecode.provider=javassist
to
hibernate.bytecode.provider=bytebuddy
CodePudding user response:
I deleted the existing javaasisst, installed it again and now it works with the newest version.