javax.persistence.Query.getSingleResult()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(848)

本文整理了Java中javax.persistence.Query.getSingleResult方法的一些代码示例,展示了Query.getSingleResult的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.getSingleResult方法的具体详情如下:
包路径:javax.persistence.Query
类名称:Query
方法名:getSingleResult

Query.getSingleResult介绍

[英]Execute a SELECT query that returns a single untyped result.
[中]执行一个SELECT查询,返回一个非类型化的结果。

代码示例

代码示例来源:origin: stackoverflow.com

  1. public User getUserByUsernameOrNull(String username) {
  2. try{
  3. Query q = em.createNamedQuery(User.getUserByUsername);
  4. q.setParameter("username", username);
  5. return (User) q.getSingleResult();
  6. } catch(NoResultException e) {
  7. return null;
  8. }
  9. }

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

  1. @Override
  2. public CustomerPayment readCustomerPaymentByToken(String token) {
  3. Query query = em.createNamedQuery("BC_READ_CUSTOMER_PAYMENT_BY_TOKEN");
  4. query.setParameter("token", token);
  5. CustomerPayment payment = null;
  6. try{
  7. payment = (CustomerPayment) query.getSingleResult();
  8. } catch (NoResultException e) {
  9. //do nothing
  10. }
  11. return payment;
  12. }

代码示例来源:origin: netgloo/spring-boot-samples

  1. /**
  2. * Return the user having the passed email.
  3. */
  4. public User getByEmail(String email) {
  5. return (User) entityManager.createQuery(
  6. "from User where email = :email")
  7. .setParameter("email", email)
  8. .getSingleResult();
  9. }

代码示例来源:origin: spring-projects/spring-integration-samples

  1. public static long countLoanSharks() {
  2. return ((Number) entityManager().createQuery("select count(o) from LoanShark o").getSingleResult()).longValue();
  3. }

代码示例来源:origin: Impetus/Kundera

  1. Object p3 = prepareMongoInstance("3", 15);
  2. Query findQuery = em.createQuery("Select p from PersonMongo p");
  3. List<PersonMongo> allPersons = findQuery.getResultList();
  4. Assert.assertNotNull(allPersons);
  5. Assert.assertTrue(allPersons.isEmpty());
  6. findQuery = em.createQuery("Select p from PersonMongo p where p.personName = vivek");
  7. allPersons = findQuery.getResultList();
  8. Assert.assertNotNull(allPersons);
  9. assertFindWithoutWhereClause(em, "PersonMongo", PersonMongo.class);
  10. findQuery = em.createQuery("Select p from PersonMongo p where p.day = :day");
  11. findQuery.setParameter("day", Day.FRIDAY);
  12. allPersons = findQuery.getResultList();
  13. Assert.assertNotNull(allPersons);
  14. PersonMongo result = (PersonMongo) (query.getSingleResult());
  15. Assert.assertNotNull(result);
  16. Assert.assertEquals(Month.JAN, result.getMonth());
  17. try
  18. result = (PersonMongo) (query.getSingleResult());
  19. Assert.fail("Should have gone to catch block!");

代码示例来源:origin: kiegroup/jbpm

  1. @Override
  2. public ExecutionError get(String errorId) {
  3. if (!isActive()) {
  4. return null;
  5. }
  6. return (ExecutionError) call((EntityManager em) -> {
  7. return em.createQuery("from ExecutionErrorInfo where errorId =:errorId")
  8. .setParameter("errorId", errorId)
  9. .getSingleResult();
  10. });
  11. }

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

  1. @Override
  2. public StaticAssetStorage readStaticAssetStorageByStaticAssetId(Long id) {
  3. Query query = em.createNamedQuery("BC_READ_STATIC_ASSET_STORAGE_BY_STATIC_ASSET_ID");
  4. query.setParameter("id", id);
  5. return (StaticAssetStorage) query.getSingleResult();
  6. }

代码示例来源:origin: hibernate/hibernate-orm

  1. private static boolean hasData(EntityManager entityManager) {
  2. return (((Long) entityManager.createQuery( "select count(a) from Address a" ).getSingleResult()) +
  3. ((Long) entityManager.createQuery( "select count(p) from Person p" ).getSingleResult())) > 0;
  4. }
  5. }

代码示例来源:origin: stackoverflow.com

  1. String sql = "SELECT USER.* FROM USER_ AS USER WHERE ID = ?";
  2. Query query = em.createNativeQuery(sql, User.class);
  3. query.setParameter(1, id);
  4. User user = (User) query.getSingleResult();

代码示例来源:origin: shopizer-ecommerce/shopizer

  1. Query q = this.em.createQuery(hql);
  2. q.setParameter("pid", productId);
  3. Product p = (Product)q.getSingleResult();

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

  1. public Role readRoleByName(String name) {
  2. Query query = em.createNamedQuery("BC_READ_ROLE_BY_NAME");
  3. query.setParameter("name", name);
  4. return (Role) query.getSingleResult();
  5. }

代码示例来源:origin: hibernate/hibernate-orm

  1. private boolean hasData() {
  2. return (((Long) entityManager.createQuery("select count(a) from Address a").getSingleResult()) +
  3. ((Long) entityManager.createQuery("select count(p) from Person p").getSingleResult())) > 0;
  4. }

代码示例来源:origin: kiegroup/jbpm

  1. continue;
  2. query.setParameter(name, params.get(name));
  3. return (T) query.getSingleResult();

代码示例来源:origin: shopizer-ecommerce/shopizer

  1. Query q = this.em.createQuery(hql);
  2. q.setParameter("cm", store.getId());
  3. q.setParameter("se", seUrl);
  4. Content content = (Content)q.getSingleResult();

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

  1. @Override
  2. public SearchIntercept findInterceptByTerm(String term) {
  3. Query query = em.createNamedQuery("BC_READ_SEARCH_INTERCEPT_BY_TERM");
  4. query.setParameter("searchTerm", term);
  5. SearchIntercept result;
  6. try {
  7. result = (SearchIntercept) query.getSingleResult();
  8. } catch (NoResultException e) {
  9. result = null;
  10. }
  11. return result;
  12. }

代码示例来源:origin: rapidoid/rapidoid

  1. @SuppressWarnings("unchecked")
  2. public <T> T getSingleResult() {
  3. Query q = JPA.em().createQuery(jpql);
  4. JPA.bind(q, namedArgs, args);
  5. return (T) q.getSingleResult();
  6. }

代码示例来源:origin: hibernate/hibernate-orm

  1. @Test
  2. public void testStoredProcedureReturnValue() {
  3. try {
  4. doInJPA( this::entityManagerFactory, entityManager -> {
  5. BigDecimal phoneCount = (BigDecimal) entityManager
  6. .createNativeQuery("SELECT fn_count_phones(:personId) FROM DUAL")
  7. .setParameter("personId", 1)
  8. .getSingleResult();
  9. assertEquals(BigDecimal.valueOf(2), phoneCount);
  10. });
  11. }
  12. catch (Exception e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. }

代码示例来源:origin: shopizer-ecommerce/shopizer

  1. Query q = this.em.createQuery(hql);
  2. q.setParameter("lid", regionList);
  3. q.setParameter("dt", new Date());
  4. q.setParameter("seUrl", seUrl);
  5. p = (Product)q.getSingleResult();
  6. } catch(javax.persistence.NoResultException ignore) {

代码示例来源:origin: kiegroup/jbpm

  1. protected AuditTaskImpl getAuditTask(EntityManager em, long taskId) {
  2. try {
  3. AuditTaskImpl auditTaskImpl = (AuditTaskImpl) em.createNamedQuery("getAuditTaskById").setParameter("taskId", taskId).getSingleResult();
  4. return auditTaskImpl;
  5. } catch (NoResultException | NonUniqueResultException e) {
  6. return null;
  7. }
  8. }
  9. }

代码示例来源:origin: kiegroup/jbpm

  1. @Override
  2. public Long findTaskIdByContentId( Long contentId ) {
  3. check();
  4. CriteriaBuilder builder = this.em.getCriteriaBuilder();
  5. CriteriaQuery<Long> query = builder.createQuery(Long.class);
  6. Root<TaskImpl> taskRoot = query.from(TaskImpl.class);
  7. Root<ContentImpl> contentRoot = query.from(ContentImpl.class);
  8. query.select(taskRoot.get(TaskImpl_.id));
  9. Predicate taskContentJoinPred = builder.equal(
  10. contentRoot.get(ContentImpl_.id),
  11. taskRoot.get(TaskImpl_.taskData).get(TaskDataImpl_.outputContentId));
  12. Predicate contentIdPred = builder.equal(
  13. contentRoot.get(ContentImpl_.id),
  14. contentId);
  15. query.where(builder.and(taskContentJoinPred, contentIdPred));
  16. Query choppedLiver = em.createQuery(query);
  17. return (Long) choppedLiver.getSingleResult();
  18. }

相关文章