簡體   English   中英

org.hibernate.exception.GenericJDBCException:無法插入:

[英]org.hibernate.exception.GenericJDBCException: could not insert:

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類

**包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: 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是您應該創建數據庫的序列的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM