简体   繁体   中英

Hibernate findOne doesn't work the first time

The following code throws a NullPointerException when id matches a form revision in the database:

@Transactional(readOnly = true)
public FormRevision getFormRevision(Long id) {
    FormRevision formRevision = formRevisionDao.findOne(id);
    formRevision.fillLazyCollections();//NullPointerException happens here for some form revisions
    return formRevision;
}

This happens because the formRevisionDao.findOne(id) is returning null for certain form revisions. This does not happen for every form revision, but for the ones it does happen for it is consistent, and will fail every time.

Additional Info:

The formRevisionDAO object is auto-generated by Hibernate and looks like the following

@Repository()
public interface IFormRevisionDAO extends JpaRepository<FormRevision, Long> {}

The generated hibernate query doesn't look like it could cause the cause. It only has left outer joins, and the where clause is only "id = ?". Also, this query works for other form revisions.

It also does not appear to be a caching issue, as I removed caching from the FormRevision object and the problem persists.

Strangest of all, if you debug the code, the first formRevisionDao.findOne(id) will return null. However, subsequent identical calls will return the expected object.


I'm not sure what else could be causing this. Maybe a synchronization or Hibernate version issue (currently using Hibernate 4.1.12 and using it with "Oracle Database 10g Release 10.2.0.4.0 - 64bit Production")?

Any help/insight would be greatly appreciated!

According to Spring Data doc , findOne:

Returns:the entity with the given id or null if none found

Throws:IllegalArgumentException - if id is null

So it only returns null when no entry is found. Being consistent and always returning null reinforces the conclusion that no record was found. Take the same db credentials and open an SQL console and check the missing record query.

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