【发布时间】:2017-11-16 18:37:22
【问题描述】:
我正在尝试使用 Hibernate 和 Spring 以 HQL 将对象保存在我的 MySQL 数据库中。 但是,保存时,出现以下错误消息:
org.springframework.web.util.NestedServletException: 请求 处理失败;嵌套异常是 java.lang.NullPointerException
public void addCountry(Country country) throws Exception {
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
//Add new Country object
Country c = new Country();
c.setId(""); //id value is auto into mySQL
c.setCity1(country.getCity1());
c.setCity2(country.getCity2());
session.save(c);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw e;
} finally {
session.close();
}
}
【问题讨论】:
-
向问题添加堆栈跟踪。指向源代码中发生异常的行。以及为什么 c.setId(""); //id 值是否自动进入mySQL 如果是auto?
-
尝试删除 c.setId("");
-
如果您需要帮助,除了堆栈跟踪之外,您还应该发布
Country的定义。 -
你确定参数国家不为空吗?
标签: java mysql spring hibernate hql