Skip to content

Commit

Permalink
fix(entity load): use session.get to prevent non-null entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Dec 12, 2016
1 parent c5a4bfb commit 29e7dc1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
Expand Up @@ -49,16 +49,23 @@ public Class<T> getPersistentClass() {
return persistentClass;
}

/**
* Use session.get() instead of session.load() to prevent non-null entity
* being return if does not exists.
*
* See {@link org.hibernate.Session#get(Class, Serializable)} and
* {@link org.hibernate.Session#load(Class, Serializable)} for documentation.
*/
@SuppressWarnings("unchecked")
@Override
public T findById(ID id, boolean lock) {
T entity;
if (lock) {
entity =
(T) getSession().load(getPersistentClass(), id,
(T) getSession().get(getPersistentClass(), id,
LockOptions.UPGRADE);
} else {
entity = (T) getSession().load(getPersistentClass(), id);
entity = (T) getSession().get(getPersistentClass(), id);
}

return entity;
Expand Down
Expand Up @@ -11,7 +11,6 @@
import java.util.zip.ZipOutputStream;

import javax.enterprise.context.RequestScoped;
import javax.persistence.EntityNotFoundException;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
Expand Down Expand Up @@ -334,19 +333,17 @@ public Response deleteEntry(Long id) {
if (response != null) {
return response;
}
try {
HGlossaryEntry entry = glossaryDAO.findById(id);
if(entry != null) {
GlossaryEntry deletedEntry = generateGlossaryEntry(entry);
glossaryDAO.makeTransient(entry);
glossaryDAO.flush();
return Response.ok(deletedEntry).build();
}
} catch (EntityNotFoundException enfe) {
// Drop to NOT FOUND response
HGlossaryEntry entry = glossaryDAO.findById(id);

if(entry != null) {
GlossaryEntry deletedEntry = generateGlossaryEntry(entry);
glossaryDAO.makeTransient(entry);
glossaryDAO.flush();
return Response.ok(deletedEntry).build();
} else {
return Response.status(Response.Status.NOT_FOUND)
.entity("Glossary entry not found: " + id).build();
}
return Response.status(Response.Status.NOT_FOUND)
.entity("Glossary entry " + id + " not found").build();
}

@Override
Expand Down

0 comments on commit 29e7dc1

Please sign in to comment.