gpt4 book ai didi

java - 刷新时出现 NullPointerException [Java EE + Hibernate 4. 3.5]

转载 作者:行者123 更新时间:2023-11-30 07:02:53 25 4
gpt4 key购买 nike

在将 JPA 注释添加到我的 bean 后,我在 session.flush() 上遇到了 NullPointerException 。也许我有错误的注释或者 hibernate 缓存中有东西?

这是我的用户实体:

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

当然!完整的 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)

已编辑:我尝试在方法flushAndClose 中放置一个try/catch block 。不,我有这个 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 哭了,所以我删除了它们。所以这就是为什么我认为缓存中有数据但如何删除它?!

最佳答案

日志中显示的问题出现在“Register.dateEnd”bean 中。我没有看到你提供了代码。错误的 bean 映射会导致您的 session 无法初始化。

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

关于java - 刷新时出现 NullPointerException [Java EE + Hibernate 4. 3.5],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40593034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com