简体   繁体   中英

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]

Table created but the values are not inserted into the tables... How can i solve this? Please help me.

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

This is my bean class... Table created to me and it is redirected to next page. but the values we entered in or not inserted in the database.

**

  • 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");
    }
}

given values are not inserted into the table. It through an exception "org.hibernate.exception.GenericJDBCException: could not insert:" Please Help me... Am waiting for your answer...

If you look at the hibernate query generated

Hibernate: insert into purchase_details (date, total_quantity, total_weight, type) values (?, ?, ?, ?)

primary key is not getting populated correctly, you have used @GeneratedValue for mapping your primary key without any strategy . So by default Hibernate uses strategy AUTO .

From Hibernate docs

AUTO - either identity column, sequence or table depending on the underlying DB

So hibernate selects the strategy based on the database you use.

May be try using sequence generator

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

where PURCHASE_SEQ is the name of the sequence you should create database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM