简体   繁体   中英

Hibernate not returning newly inserted record

I am developing one web application using following frameworks :

Spring 4.2.4.RELEASE
Hibernate 4.3.6.Final
MySQL Connector 5.1.31
Jackson 2.6.5
MySQL 5.7

I am creating a new record on the basis of user inputs captured via JSP.

Below is the code for inserting a new record using hibernate

public Record addRecord(Record record) {
    logger.info("Persisting new record : " + record);
    Record createdRecord = null;
    SessionFactory hibSessionFactory = HibernateUtil.getSessionFactory();
    Session hibSession = hibSessionFactory.openSession();
    Transaction hibTransaction = hibSession.beginTransaction();
    int record ID = hibSession.save(record);
    hibTransaction.commit();
    hibSession.flush();
    hibSession.close();
    logger.info("Persisted new record : " + createdRecord);
    createdRecord = getRecordById(recordID);
    logger.info("Created record : " + createdRecord);
    return createdRecord;
}

I am using page redirect on the same page after inserting new after calling get all records. But problem I am facing after redirect it is not showing all records except the latest record. But when I do a page refresh it shows me all records including the latest record.

Below is the controller code :-

@RequestMapping(value = "records/addRecord", method = RequestMethod.POST)
@ResponseBody
public void addRecord(HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException {
    String recordId = request.getParameter("record_id");
    String recordName = request.getParameter("record_name");
    String recordAddress = request.getParameter("record_address");
    String recordCity = request.getParameter("record_city");
    String recordState = request.getParameter("record_state");
    int recordZip = Integer.parseInt(request.getParameter("record_zip"));
    Record newRecord = new Record();
    newRecord.setRecordID(Integer.parseInt(recordId));
    newRecord.setRecordName(recordName);
    newRecord.setRecordAddress(recordAddress);
    newRecord.setRecordCity(recordCity);
    newRecord.setRecordState(recordState);
    newRecord.setRecordZIP(recordZip);
    logger.info("Received request to add record : " + newRecord);
    Record addedRecord = recordService.addRecord(newRecord);
    logger.info("Request for adding record completed, added record - " + addedrecord);
    response.setHeader("Cache-Control", "private, no-store, no-cache, must-revalidate");
    response.sendRedirect("/RecordApp/records.jsp");
}

NOTE : There is no caching used in the hibernate configuration file.

Record before you apply the save method is a transient object, once you apply

hibSession.save(record);

it save it in the database, but this save method returns the Serializable identifier for that record, what you could do is use identifier and get it from the database, do this createdRecord = record; is basically pass the same object to the return value

Integer id = hibSession.save(record);
Record createdRecord = hibSession.get(Record.class,id);
return createdRecord

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