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 ?

Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44
nand
  • 517
  • 2
  • 13
  • 29
  • How are you creating your session factory? – John Ament Jul 05 '15 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 – nand Jul 05 '15 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 – nand Jul 05 '15 at 13:47

2 Answers2

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

Community
  • 1
  • 1
Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44
  • 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. – nand Jul 05 '15 at 14:53
  • your solution work . db takes time to reflect inserted data :-) – nand Jul 06 '15 at 17:58
  • @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.. :-) – Viraj Nalawade Jul 07 '15 at 00:01
  • @Nand yes we can !! Just mention the stackoverflow refernce there so that it reminds me.. – Viraj Nalawade Jul 07 '15 at 06:36
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?

Community
  • 1
  • 1
pappu_kutty
  • 2,378
  • 8
  • 49
  • 93
  • 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. – nand Jul 05 '15 at 14:56