0

I am getting below error while working on the Hibernate code to insert record in the database. Please suggest what wrong I am doing here. Hibernate: insert into feedback (fb_rating_id, fb_suggestion, fb_topic_id, user_info) values (?, ?, ?, ?) Exception deleting record :org.hibernate.exception.GenericJDBCException: could not insert: [com.tdp.model.Feedback]

     public int insertFeedback(String ratingId, String msg) {
     int status = 0;

      Session sess = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = sess.beginTransaction();
            //Criteria crit = sess.createCriteria(Feedback.class);
          //  crit.add(Restrictions.eq("feedbackId",feedback.getFeedbackId()));
            Feedback fbObj = new Feedback();
            fbObj.setFbRatingId(Integer.parseInt(ratingId));
            fbObj.setFbSuggestion(msg);
            fbObj.setFbTopicId(null);
            fbObj.setUserInfo("");

            sess.save(fbObj);
            tx.commit();
            status  = 1;
        } catch (HibernateException e) {
            System.out.println("Exception deleting record :" + e);
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            sess.close();
        }        
     return status;

}
     Feedback.java
        @Table(name=TDPConstants.FEEDBACK )
        public class Feedback {

        private Integer feedbackId;  //primary key
        private Integer fbRatingId;
        private Integer fbTopicId;
        private String fbSuggestion ;
        private String userInfo ;
         @Id
         @Column(name = "feedback_id",unique = true, nullable = false)
         @GeneratedValue(strategy = GenerationType.AUTO)
          public Integer getFeedbackId() {
          return feedbackId;
          }

         public void setFeedbackId(Integer feedbackId) {
         this.feedbackId = feedbackId;
          }
        //all other setters & getters
MSV
  • 155
  • 2
  • 16
  • if it's as you've stated, that there is indeed an error then surely the compiler will give feedback regarding the error. please paste the error log. – Ousmane D. Apr 04 '17 at 14:00
  • http://stackoverflow.com/questions/40720799/deprecated-createcriteria-method-in-hibernate – mft Apr 04 '17 at 14:08

1 Answers1

0

Could it be because you have the conditions commented out and so it is returning more than 1 object? Hibernate throws an exception if you told it to expect 1 result but it has multiple results or no result.

Based on your comment You have to create a new instance of Feedback and set the data on it and then pass that instance to Hibernate to insert into the database.

Daniel Bickler
  • 1,119
  • 13
  • 29
  • This is going to be my first record in the table. I guess with criteria I can't insert record in the DB. Please correct me. Is there any other way to do this? – MSV Apr 04 '17 at 14:03
  • The error then is there is no result, again because you told Hibernate to expect 1. You have to create a new instance of Feedback and set the data on it and then pass that instance to Hibernate to insert into the database. – Daniel Bickler Apr 04 '17 at 14:05
  • Yes I have update the code to create feedback obj and set the values but now I am getting error - Hibernate: insert into feedback (fb_rating_id, fb_suggestion, fb_topic_id, user_info) values (?, ?, ?, ?) Exception deleting record :org.hibernate.exception.GenericJDBCException: could not insert: [com.tdp.model.Feedback] – MSV Apr 04 '17 at 17:26
  • That is almost a separate question, but can you move the full error/stack trace into your question? – Daniel Bickler Apr 04 '17 at 17:40