patternjavaMinor
Checking if entity exist in Google App Engine Datastore using JPA 1
Viewed 0 times
enginegoogleappcheckingexistusingdatastoreentityjpa
Problem
Below is my method in checking if entity already exist.
Is the code above ok? Please suggest.
public boolean isExist(String genreName) throws PersistenceException{
EntityManager em = EMF.get().createEntityManager();
boolean flag = false;
try{
Genre genre = em.find(Genre.class, genreName);
if (genre != null)
flag = true;
}finally{
em.close();
}
return flag;
}Is the code above ok? Please suggest.
Solution
It looks fine. You could omit the boolean flag if you return immediately when you know the answer:
public boolean isExist(final String genreName) throws PersistenceException {
final EntityManager em = EMF.get().createEntityManager();
try {
final Genre genre = em.find(Genre.class, genreName);
if (genre != null) {
return true;
}
return false;
} finally {
em.close();
}
}Code Snippets
public boolean isExist(final String genreName) throws PersistenceException {
final EntityManager em = EMF.get().createEntityManager();
try {
final Genre genre = em.find(Genre.class, genreName);
if (genre != null) {
return true;
}
return false;
} finally {
em.close();
}
}Context
StackExchange Code Review Q#15704, answer score: 5
Revisions (0)
No revisions yet.