3

I have a single table in db . Created a pojo class to map class instance to table. my class structure is

   @Entity
    @Table(name = "example")
    class example {
     @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "ID")
        int id;
        @Column(name = "SLOT_ID")
        int slot_id;
        @Column(name = "SLOT_DATE")
        String slot_date;
        @Column(name = "HOTEL_ID")
        String hotel_id;
        @Column(name = "ROOM_TYPE_ID")
        String room_type_id;
        @Column(name = "CREATE_DATE")
        String create_date;
        @Column(name = "UPDATE_DATE")
        String update_date;
        @Column(name = "SLOT_PRICE")
        Double slot_price;
        @Column(name = "AVAILABLE_ROOMS")
        Integer available_roooms;

//rest have getter and setter method }

Hibernet commit part

  public void save(Example example) {

        Session session = null;
        try {
            log.info( example.toString());
            session = this.sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
            session.persist(example);
            tx.commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

in log i am getting this log

Hibernate: insert into example(AVAILABLE_ROOMS, CREATE_DATE, HOTEL_ID, ROOM_TYPE_ID, SLOT_DATE, SLOT_ID, SLOT_PRICE, UPDATE_DATE) values (?, ?, ?, ?, ?, ?, ?, ?)

I am able to fetch data from same table here is Code snap `

session = this.sessionFactory.openSession(); 
Criteria cr = session.createCriteria(Example.class); cr.add(Restrictions.eq("id", id)); List results = cr.list();
 if(results.size()>0) 
return mapper.writeValueAsString(results.get(0)); //id is auto //incremented in table`

I dont see any error in log but when i cheked in DB no data has been inserted.Any clue what i missed ?

3
  • How are you creating your session factory? Commented Jul 5, 2015 at 13:01
  • Hi John, i have defined session factory as a bean then it autowired with implmentation @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } I have checked , i am getting instance of sessionFactory Commented Jul 5, 2015 at 13:04
  • Hi John, I am able to fetch data from same table here is Code snap session = this.sessionFactory.openSession(); Criteria cr = session.createCriteria(Example.class); cr.add(Restrictions.eq("id", id)); List results = cr.list(); if(results.size()>0) return mapper.writeValueAsString(results.get(0)); //id is auto //incremented in table Commented Jul 5, 2015 at 13:47

2 Answers 2

4

Use this code and test once

public void save(Example example) {
    Session session = null;
    Transaction tx=null;
    try {
        log.info( example.toString());
        session = this.sessionFactory.openSession();
        tx = session.beginTransaction();
        session.persist(example);
        tx.commit();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {

     if (!tx.wasCommitted()) {
     tx.rollback();
     }//not much doing but a good practice
     session.flush(); //this is where I think things will start working.
     session.close();
    }
}

Good reason to call flush before close is here

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

4 Comments

Hi Viraj , I have tried but it didn't work. I try also try one more thing is . create a new table without any auto inremented field and then insert dummy data (Hibernate: insert into person (name, id) values (?, ?) ) . I cheked insertion is happeing so i am thinking that auto incremented field could be issue. if do you feel same then can you please suggest me some way to fix this issue.
your solution work . db takes time to reflect inserted data :-)
@Nand Yes it can take time and also hibernate takes time to flush it too.. Glad it worked.. You can accept this answer if my solution worked.. :-)
@Nand yes we can !! Just mention the stackoverflow refernce there so that it reminds me..
2

i think you have to use session.save() instead of session.persist(), or you have to use flush at the end of transaction as pointed by Viraj, also refer this post

What's the advantage of persist() vs save() in Hibernate?

1 Comment

Hi pappu, I am using "session.persist" and also tested code provided by viraj that didn't work. I think issue with auto incremented field defined in table. Can you please show me some way to fix that.

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.