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

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

本文整理了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

public User getUserByUsernameOrNull(String username) {
  try{
    Query q = em.createNamedQuery(User.getUserByUsername);
    q.setParameter("username", username);
    return (User) q.getSingleResult();
  } catch(NoResultException e) {
    return null;
  }
}

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

@Override
public CustomerPayment readCustomerPaymentByToken(String token) {
  Query query = em.createNamedQuery("BC_READ_CUSTOMER_PAYMENT_BY_TOKEN");
  query.setParameter("token", token);
  CustomerPayment payment = null;
  try{
    payment = (CustomerPayment) query.getSingleResult();
  } catch (NoResultException e) {
    //do nothing
  }
  return  payment;
}

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

/**
 * Return the user having the passed email.
 */
public User getByEmail(String email) {
 return (User) entityManager.createQuery(
   "from User where email = :email")
   .setParameter("email", email)
   .getSingleResult();
}

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

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

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

Object p3 = prepareMongoInstance("3", 15);
Query findQuery = em.createQuery("Select p from PersonMongo p");
List<PersonMongo> allPersons = findQuery.getResultList();
Assert.assertNotNull(allPersons);
Assert.assertTrue(allPersons.isEmpty());
findQuery = em.createQuery("Select p from PersonMongo p where p.personName = vivek");
allPersons = findQuery.getResultList();
Assert.assertNotNull(allPersons);
assertFindWithoutWhereClause(em, "PersonMongo", PersonMongo.class);
findQuery = em.createQuery("Select p from PersonMongo p where p.day = :day");
findQuery.setParameter("day", Day.FRIDAY);
allPersons = findQuery.getResultList();
Assert.assertNotNull(allPersons);
PersonMongo result = (PersonMongo) (query.getSingleResult());
Assert.assertNotNull(result);
Assert.assertEquals(Month.JAN, result.getMonth());
try
  result = (PersonMongo) (query.getSingleResult());
  Assert.fail("Should have gone to catch block!");

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

@Override
public ExecutionError get(String errorId) {
  if (!isActive()) {
    return null;
  }
  return (ExecutionError) call((EntityManager em) -> {
    
    return em.createQuery("from ExecutionErrorInfo where errorId =:errorId")
      .setParameter("errorId", errorId)
      .getSingleResult();
  });
}

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

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

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

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

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

String sql = "SELECT USER.* FROM USER_ AS USER WHERE ID = ?";

Query query = em.createNativeQuery(sql, User.class);
query.setParameter(1, id);
User user = (User) query.getSingleResult();

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

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

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

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

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

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

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

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

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

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

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

@Override
public SearchIntercept findInterceptByTerm(String term) {
  Query query = em.createNamedQuery("BC_READ_SEARCH_INTERCEPT_BY_TERM");
  query.setParameter("searchTerm", term);
  SearchIntercept result;
  try {
    result = (SearchIntercept) query.getSingleResult();
  } catch (NoResultException e) {
    result = null;
  }
  return result;
}

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

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

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

@Test
  public void testStoredProcedureReturnValue() {
    try {
      doInJPA( this::entityManagerFactory, entityManager -> {
        BigDecimal phoneCount = (BigDecimal) entityManager
            .createNativeQuery("SELECT fn_count_phones(:personId) FROM DUAL")
            .setParameter("personId", 1)
            .getSingleResult();
        assertEquals(BigDecimal.valueOf(2), phoneCount);
      });
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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

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

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

protected AuditTaskImpl getAuditTask(EntityManager em, long taskId) {
    try {
      AuditTaskImpl auditTaskImpl = (AuditTaskImpl) em.createNamedQuery("getAuditTaskById").setParameter("taskId", taskId).getSingleResult();                
      
      return auditTaskImpl;
    } catch (NoResultException | NonUniqueResultException e) {
      return null;
    }
  }
}

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

@Override
public Long findTaskIdByContentId( Long contentId ) {
  check();
  CriteriaBuilder builder = this.em.getCriteriaBuilder();
  CriteriaQuery<Long> query = builder.createQuery(Long.class);
  Root<TaskImpl> taskRoot = query.from(TaskImpl.class);
  Root<ContentImpl> contentRoot = query.from(ContentImpl.class);
  query.select(taskRoot.get(TaskImpl_.id));
  Predicate taskContentJoinPred = builder.equal(
      contentRoot.get(ContentImpl_.id),
      taskRoot.get(TaskImpl_.taskData).get(TaskDataImpl_.outputContentId));
  Predicate contentIdPred = builder.equal(
      contentRoot.get(ContentImpl_.id),
      contentId);
  query.where(builder.and(taskContentJoinPred, contentIdPred));
  Query choppedLiver = em.createQuery(query);
  return (Long) choppedLiver.getSingleResult();
}

相关文章