简体   繁体   English

Hibernate没有返回新插入的记录

[英]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.

相关问题 休眠如何在 session.save(object) 上获取新插入的记录主键 - How is hibernate getting the newly inserted record primary key on session.save(object) 创建Trigger更新PostgreSQL上新插入的记录 - Create Trigger to update the record that was newly inserted on PostgreSQL 从 hibernate 插入记录时,触发器未更新当前记录 PostgresSQL - Trigger is not updating current record PostgresSQL when record is inserted from hibernate 在MongoDb中获取/返回新插入或新修改的文​​档(使用Java驱动程序) - Getting/returning the newly-inserted or newly-modified document in MongoDb (with java driver) hibernate 插入新记录时未更新缓存查询 - hibernate cached query not updated when new record inserted 如何在Hibernate中使用JPA从表中选择最后插入的5条记录 - How to select last inserted 5 record from table using JPA with Hibernate 休眠:ManyToMany单向仅返回一个记录 - Hibernate: ManyToMany unidirectional only returning one record 在Android中获取新插入的联系人 - Getting newly inserted contacts in Android Hibernate在同一事务中新创建的记录上的save()之后调用get() - Hibernate calling get() after save() on the newly created record within the same transaction 当结果为一条记录时,Hibernate Search / Lucene返回空元素 - Hibernate Search / Lucene returning null elements when result is one record
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM