log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into purchase_details (date, total_quantity, total_weight, type) values (?, ?, ?, ?)
HE:org.hibernate.exception.GenericJDBCException: could not insert: [com.focus.beans.PurchaseDetailsList]
已创建表,但值未插入表中...我该如何解决此问题?
package com.focus.beans;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
@Entity
@Table(name = "purchase_details")
public class PurchaseDetailsList {
@Id @GeneratedValue
@Column (name="type_id")
@Getter @Setter private String type_id;
@Column (name = "type")
@Getter @Setter private String type;
@Column(name = "date")
@Getter @Setter private String date;
@Column (name = "total_quantity")
@Getter @Setter private String totalquantity;
@Column (name = "total_weight")
@Getter @Setter private String totalweight;
}
这是我的bean类......表被创建并被重定向到下一页,但是我们输入的值没有插入到数据库中。
MyController.class
package com.focus.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.focus.beans.PurchaseDetailsList;
import com.focus.dao.Dao;
public class PurchaseDetail extends HttpServlet {
private static final long serialVersionUID = 1L;
PrintWriter out = null;
boolean flag;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Dao dao = Dao.getInstance();
PurchaseDetailsList pdl = new PurchaseDetailsList();
pdl.setType(request.getParameter("types"));
pdl.setDate(request.getParameter("date"));
pdl.setTotalquantity(request.getParameter("totalquantity"));
pdl.setTotalweight(request.getParameter("totalweight"));
//System.out.println("Am arun");
flag = dao.persist(pdl);
response.getWriter();
response.sendRedirect("JSP/PurchaseDetailsListForm.jsp");
}
}
但是给定的值没有插入到表中,并且抛出异常:
org.hibernate.exception.GenericJDBCException: could not insert:
我该如何修复它?
如果你查看生成的休眠查询
Hibernate: insert into purchase_details (date, total_quantity, total_weight, type) values (?, ?, ?, ?)
primary key
未正确填充,您已使用 @GeneratedValue
来映射您的 primary key
,而没有任何 strategy
。
因此默认情况下 Hibernate
使用策略 AUTO
。
来自 Hibernate 文档
AUTO - either identity column, sequence or table depending on the underlying DB
因此
hibernate
根据您使用的数据库选择策略。
可以尝试使用
sequence generator
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="purchase_seq")
@SequenceGenerator(name="purchase_seq", sequenceName="PURCHASE_SEQ")
private String type_id;
其中
PURCHASE_SEQ
是您应创建数据库的序列的名称。