2016-11-14 70 views
1

我有和NullPointerExceptionsession.flush()後,我已經添加JPA註釋到我的豆。也許我有錯誤的註釋或有什麼在休眠緩存?NullPointerException在刷新[Java EE +休眠4. 3.5]

有我的用戶實體:

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; 
    } 
} 

的道是:

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

HibernateUtil的代碼:

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(); 
    } 
} 

和代碼最後部分,我添加一個新用戶:

// 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:創建方法:

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

AND:flushAndClose()方法

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

,當然!完全鬆懈!

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) 

編輯:我試圖把一個try/catch塊內的方法flushAndClose。 號我有這個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) 

之前,我加入@Temporal註解和我已經刪除,因爲他們的IntelliJ哭了。所以這就是爲什麼我認爲緩存中有數據但是如何刪除它?!

+0

請告訴我們'cr​​eate'方法和'flushAndClose'方法。 – Shadov

+3

'session'屬性似乎爲空。你如何檢索它? –

+0

'session.saveOrUpdate()'沒有錯誤? – Shadov

回答

1

日誌中顯示的問題出現在「Register.dateEnd」bean中。 我沒看到你提供的代碼。錯誤的bean映射會導致您的會話無法初始化。

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

謝謝!問題已經解決了 –