Object getSingleResult() throws NoResultException, NonUniqueResultException
EntityManager em = Persistence.createEntityManagerFactory("myPU").createEntityManager(); Query query = em.createQuery("SELECT e FROM Employee e WHERE e.empId = :empId"); query.setParameter("empId", 100); Employee emp = (Employee) query.getSingleResult();
EntityManager em = Persistence.createEntityManagerFactory("myPU").createEntityManager(); Query query = em.createQuery("SELECT COUNT(e) FROM Employee e"); Long count = (Long) query.getSingleResult();This code retrieves the count of Employee entity objects from the database. Package Library: The javax.persistence package library is used for object-relational mapping (ORM) in Java. It provides a set of classes and interfaces that allow developers to map Java objects to relational database tables. It is included in the Java Persistence API (JPA) specification, which is part of the Java EE platform.