[英]Hibernate not returning newly inserted record
I am developing one web application using following frameworks : 我正在使用以下框架开发一个Web应用程序:
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. 我正在基于通过JSP捕获的用户输入创建新记录。
Below is the code for inserting a new record using hibernate 下面是使用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. 在调用get all records后插入new后,我在同一页面上使用页面重定向。 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. 注意:hibernate配置文件中没有使用缓存。
Record before you apply the save
method is a transient object, once you apply 应用
save
方法之前的记录是一个临时对象,一旦你申请
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;
它将它保存在数据库中,但是这个save
method
返回该记录的Serializable
标识符,你可以做的是使用标识符并从数据库中获取它,执行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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.