gpt4 book ai didi

java - org.hibernate.exception.GenericJDBCException : could not insert:

转载 作者:行者123 更新时间:2023-11-29 00:13:04 31 4
gpt4 key购买 nike

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 类...为我创建的表,它被重定向到下一页。但我们输入或未插入数据库的值。

**

  • 我的 Controller 类

** 包 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:无法插入:” 请帮助我...正在等待您的答复...

最佳答案

如果你查看生成的 hibernate 查询

Hibernate:插入 purchase_details (date, total_quantity, total_weight, type) values (?, ?, ?, ?)

primary key 未正确填充,您已使用 @GeneratedValue 映射您的 primary key 而没有任何 strategy。所以默认情况下 Hibernate 使用策略 AUTO

来自 Hibernate docs

AUTO - 标识列、序列或表,具体取决于基础数据库

因此 hibernate 根据您使用的数据库选择策略。

可以尝试使用序列生成器

@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="purchase_seq")
@SequenceGenerator(name="purchase_seq", sequenceName="PURCHASE_SEQ")
private String type_id;

其中 PURCHASE_SEQ 是您应该创建数据库的序列的名称。

关于java - org.hibernate.exception.GenericJDBCException : could not insert:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24078153/

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