Reputation: 63
We have a java web application deployed into payara 164 application Server.
Host machine is a debian 9.3 server, used database is a mariadb-server-10.1, java version is "1.8.0_131".
Recently we are facing some problems among which the one below. It is just an instance since it happens randomly with a lot of other Objects even though indeed they are not NULL but correctly populated.
javax.ejb.EJBTransactionRolledbackException at dmlm.models.entities.User.getCustomer(User.java:337) Caused by: javax.persistence.NoResultException: getSingleResult() did not retrieve any entities.
EDIT:
in several place is called the following method:
public Customer getCustomerByUser(User user)
{
TypedQuery<Customer> q = em.createNamedQuery("Customer.findByUser", Customer.class);
q.setParameter("userId", user.getId());
if (LOG.isLoggable(Level.FINER)) {
//<editor-fold defaultstate="collapsed" desc="per stampare nei log una query runtime">
org.eclipse.persistence.sessions.Session session
= em.unwrap(JpaEntityManager.class).getActiveSession();
DatabaseQuery databaseQuery = ((EJBQueryImpl) q).getDatabaseQuery();
databaseQuery.prepareCall(session, new DatabaseRecord());
DatabaseRecord recordWithValues = new DatabaseRecord();
recordWithValues.add(new DatabaseField("userId"), user.getId());
String sqlStringWithArgs
= databaseQuery.getTranslatedSQLString(session, recordWithValues);
LOG.finest(sqlStringWithArgs);
//</editor-fold>
}
return q.getSingleResult();
}
the log part is just to print out the real query created with parameter
the named query:
@NamedQuery(name = "Customer.findByUser",
query = "SELECT m FROM Customer m JOIN m.users us WHERE m.deleted = false AND us.id = :userId")
As said, the result should be a single result with customer id
EDIT 2: We have tried to change the db from maria-db to mysql, with no success
Upvotes: 2
Views: 5113
Reputation: 1754
You should use getSingleResult()
only in case you are 100% sure that particular record exist in database (uniquely). The thrown exception is runtime. Runtime exception = programmer issue.
If you do not know if record uniquely exists you can use this construct.
public Entity getEntityByName(String name) {
TypedQuery<Entity> query = em.createNamedQuery(Entity.GET_ENTITY_BY_NAME, Entity.class);
query.setParameter(Entity.PRM_NAME, name);
List<Project> found = query.getResultList();
if (found.isEmpty()) {
return null; //or throw checked exception data not found
} else {
return found.get(0);
}
}
Upvotes: 4