2
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?

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… The table is created and it is redirected to the next page, but the values we entered in are not inserted into 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");
    }
}

But the given values are not inserted into the table, and an exception is thrown:

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

How can I fix it?

2
  • 2
    Please post complete error stack trace. Commented Jun 6, 2014 at 9:13
  • @R.S This the error.. none other in the console Commented Jun 6, 2014 at 9:15

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You are making an assumption: that the table doesn't have an identity column. If this table has an identity column, the generated query is ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.