Grechka Vassili Grechka Vassili - 24 days ago 6
Java Question

NullPointerException on flush [Java EE + Hibernate 4. 3.5]

I'm having and

NullPointerException
on
session.flush()
after I have added JPA annotations to my beans. Maybe I have wrong annotation or there is something in the hibernate cache ?

There is my User entity :

package com.viktor.solodoukhin.beans;


import javax.persistence.*;
import java.sql.Timestamp;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.util.List;

@Entity
@Table(name="tuser")
@Inheritance(strategy = InheritanceType.JOINED)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="user_id", unique=true, nullable=false)
private Long id;

@Column(name="first_name", nullable=false)
private String firstName;

@Column(name="last_name", nullable=false)
private String lastName;

@Column(name="email", unique=true)
private String email;

@Column(name="password")
private String password;

@Column(name="born_on")
private Date bornOn;

@Column(name="created_at")
private Timestamp createdAt;

@Column(name="updated_at")
private Timestamp updatedAt;

@Column(name="is_archived")
private short isArchived;

@Column(name="is_validated")
private short isValidated;

@OneToMany(mappedBy = "userId")
private List<Address> addresses;

public Long getId() {
return id;
}

public void setId(Long 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;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
String hashedPassword = null;
try{
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes(), 0, password.length());
hashedPassword = new BigInteger(1, md.digest()).toString(16);
this.password = hashedPassword;

} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
}
}

public Date getBornOn() {
return bornOn;
}

public void setBornOn(Date bornOn) {
this.bornOn = bornOn;
}

public Timestamp getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Timestamp createdAt) {
this.createdAt = createdAt;
}

public Timestamp getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(Timestamp updatedAt) {
this.updatedAt = updatedAt;
}

public short getIsArchived() {
return isArchived;
}

public void setIsArchived(short isArchived) {
this.isArchived = isArchived;
}

public short getIsValidated() {
return isValidated;
}

public void setIsValidated(short isValidated) {
this.isValidated = isValidated;
}

public List<Address> getAddresses() {
return addresses;
}

public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
}


The Dao is :

protected void saveOrUpdate(Object obj){
try{
startOperation();
session.saveOrUpdate(obj);
transaction.commit();
} catch(HibernateException e){
e.printStackTrace();
} finally {
flushAndClose();
}
}


HibernateUtil code :

public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}


And finally the part of code where I add a new user :

// We create our user
UserDao userDao = new UserDao();
User fabulous = new User();

fabulous.setFirstName(firstName);
fabulous.setLastName(lastName);
fabulous.setEmail(email);
fabulous.setPassword(password);
//fabulous.setCreatedAt(dt);

userDao.create(fabulous);


PS : Create method :

(UserDao extends Dao)
public void create(User user) {
super.saveOrUpdate(user);
}


AND : flushAndClose() Method

private void flushAndClose(){
session.flush();
session.close();
}


AND OF COURSE ! The complete slacktrace !

java.lang.NullPointerException
com.viktor.solodoukhin.dao.Dao.flushAndClose(Dao.java:71)
com.viktor.solodoukhin.dao.Dao.saveOrUpdate(Dao.java:20)
com.viktor.solodoukhin.dao.UserDao.create(UserDao.java:14)
com.viktor.solodoukhin.forms.UserRegisterForm.registerUser(UserRegisterForm.java:91)
com.viktor.solodoukhin.servlets.Register.doPost(Register.java:26)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


EDITED : I've tried to put a try/catch block inside the method flushAndClose.
Nos I have this slacktrace :

javax.servlet.ServletException: L''exécution de la servlet a lancé une exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

caused by

java.lang.ExceptionInInitializerError
com.viktor.solodoukhin.HibernateUtil.HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
com.viktor.solodoukhin.HibernateUtil.HibernateUtil.<clinit>(HibernateUtil.java:7)
com.viktor.solodoukhin.dao.Dao.startOperation(Dao.java:66)
com.viktor.solodoukhin.dao.Dao.saveOrUpdate(Dao.java:14)
com.viktor.solodoukhin.dao.UserDao.create(UserDao.java:14)
com.viktor.solodoukhin.forms.UserRegisterForm.registerUser(UserRegisterForm.java:91)
com.viktor.solodoukhin.servlets.Register.doPost(Register.java:26)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

caused by

org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property: com.viktor.solodoukhin.beans.Register.dateEnd
org.hibernate.cfg.annotations.SimpleValueBinder.setType(SimpleValueBinder.java:179)
org.hibernate.cfg.annotations.PropertyBinder.makePropertyAndValue(PropertyBinder.java:195)
org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:216)
org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2238)
org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963)
org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796)
org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788)
org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742)
org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
com.viktor.solodoukhin.HibernateUtil.HibernateUtil.buildSessionFactory(HibernateUtil.java:12)
com.viktor.solodoukhin.HibernateUtil.HibernateUtil.<clinit>(HibernateUtil.java:7)
com.viktor.solodoukhin.dao.Dao.startOperation(Dao.java:66)
com.viktor.solodoukhin.dao.Dao.saveOrUpdate(Dao.java:14)
com.viktor.solodoukhin.dao.UserDao.create(UserDao.java:14)
com.viktor.solodoukhin.forms.UserRegisterForm.registerUser(UserRegisterForm.java:91)
com.viktor.solodoukhin.servlets.Register.doPost(Register.java:26)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Before, I added @Temporal annotations and I've removed them because Intellij was crying. So this is why I think that there is data in cache but how to remove it ?!

Answer

The problem as show in the logs is in "Register.dateEnd" bean. I did not see that you provided the code. Wrong mapping of the bean causes that your session is not able to initialize.

org.hibernate.AnnotationException: @Temporal should only be set on a       java.util.Date or java.util.Calendar property: com.viktor.solodoukhin.beans.Register.dateEnd