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

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

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

Query.setParameter介绍

[英]Bind an argument value to a positional parameter.
[中]将参数值绑定到位置参数。

代码示例

代码示例来源:origin: spring-projects/spring-data-examples

  1. @Override
  2. @SuppressWarnings("unchecked")
  3. public List<Employee> findSubordinates(Employee manager) {
  4. return entityManager.createQuery("SELECT u from User u where u.manager = :manager") //
  5. .setParameter("manager", manager) //
  6. .getResultList();
  7. }
  8. }

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

  1. @Override
  2. public CategoryXref readXrefByIds(Long categoryId, Long subCategoryId) {
  3. Query query = em.createNamedQuery("BC_READ_CATEGORY_XREF_BY_IDS");
  4. query.setParameter("categoryId", categoryId);
  5. query.setParameter("subCategoryId", subCategoryId);
  6. return (CategoryXref) query.getSingleResult();
  7. }

代码示例来源: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: 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();
  5. List<Content> results = q.getResultList();
  6. if (results.isEmpty()) {
  7. return null;
  8. } else if (results.size() >= 1) {
  9. content = results.get(0);

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

  1. @SuppressWarnings("unchecked")
  2. public CreditCardPayment findCreditCardPayment(String referenceNumber) {
  3. Query query = em.createNamedQuery("BC_READ_CREDIT_CARD_BY_REFERENCE_NUMBER");
  4. query.setParameter("referenceNumber", referenceNumber);
  5. List<CreditCardPayment> infos = query.getResultList();
  6. CreditCardPayment response = (infos == null || infos.size() == 0) ? null : infos.get(0);
  7. if (response != null) {
  8. response.setEncryptionModule(encryptionModule);
  9. }
  10. return response;
  11. }

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

  1. @Test
  2. public void test_sql_jpa_query_parameters_example() {
  3. doInJPA( this::entityManagerFactory, entityManager -> {
  4. //tag::sql-jpa-query-parameters-example[]
  5. List<Person> persons = entityManager.createNativeQuery(
  6. "SELECT * " +
  7. "FROM Person " +
  8. "WHERE name like :name", Person.class )
  9. .setParameter("name", "J%")
  10. .getResultList();
  11. //end::sql-jpa-query-parameters-example[]
  12. assertEquals(1, persons.size());
  13. });
  14. }

代码示例来源: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: AxonFramework/AxonFramework

  1. @Override
  2. public void releaseClaim(String processorName, int segment) {
  3. EntityManager entityManager = entityManagerProvider.getEntityManager();
  4. int updates = entityManager.createQuery(
  5. "UPDATE TokenEntry te SET te.owner = null " +
  6. "WHERE te.owner = :owner AND te.processorName = :processorName " +
  7. "AND te.segment = :segment"
  8. ).setParameter("processorName", processorName).setParameter("segment", segment).setParameter("owner", nodeId)
  9. .executeUpdate();
  10. if (updates == 0) {
  11. logger.warn("Releasing claim of token {}/{} failed. It was not owned by {}", processorName, segment,
  12. nodeId);
  13. }
  14. }

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

  1. @Override
  2. public int executeUpdate(String queryName, Map<String, Object> params) {
  3. check();
  4. Query query = this.em.createNamedQuery(queryName);
  5. if (params != null) {
  6. for (Map.Entry<String, Object> paramEntry : params.entrySet()) {
  7. query.setParameter(paramEntry.getKey(), paramEntry.getValue());
  8. }
  9. }
  10. return query.executeUpdate();
  11. }

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

  1. String orderLockKey = getOrderLockKey();
  2. Query q = em.createNamedQuery("BC_ORDER_LOCK_READ");
  3. q.setParameter("orderId", order.getId());
  4. q.setParameter("key", orderLockKey);
  5. q.setHint(QueryHints.HINT_CACHEABLE, false);
  6. Long count = (Long) q.getSingleResult();
  7. q = em.createNamedQuery("BC_ORDER_LOCK_ACQUIRE");
  8. q.setParameter("orderId", order.getId());
  9. q.setParameter("currentTime", System.currentTimeMillis());
  10. q.setParameter("key", orderLockKey);
  11. Long orderLockTimeToLive = getDatabaseOrderLockTimeToLive();
  12. q.setParameter("timeout", orderLockTimeToLive==-1L?orderLockTimeToLive:System.currentTimeMillis() - orderLockTimeToLive);
  13. q.setHint(QueryHints.HINT_CACHEABLE, false);
  14. int rowsAffected = q.executeUpdate();

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

  1. @Override
  2. public ChallengeQuestion readChallengeQuestionById(long challengeQuestionId) {
  3. Query query = em.createNamedQuery("BC_READ_CHALLENGE_QUESTION_BY_ID");
  4. query.setParameter("question_id", challengeQuestionId);
  5. List<ChallengeQuestion> challengeQuestions = query.getResultList();
  6. return challengeQuestions == null || challengeQuestions.isEmpty() ? null : challengeQuestions.get(0);
  7. }

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

  1. @Test
  2. public void test_jpql_api_positional_parameter_example() {
  3. doInJPA( this::entityManagerFactory, entityManager -> {
  4. //tag::jpql-api-positional-parameter-example[]
  5. Query query = entityManager.createQuery(
  6. "select p " +
  7. "from Person p " +
  8. "where p.name like ?1" )
  9. .setParameter( 1, "J%" );
  10. //end::jpql-api-positional-parameter-example[]
  11. });
  12. }

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

  1. Query q = this.em.createQuery(hql);
  2. q.setParameter("pid", productId);
  3. q.setParameter("lid", regionList);
  4. q.setParameter("dt", new Date());
  5. q.setParameter("lang", language.getId());
  6. List<Product> results = q.getResultList();
  7. if (results.isEmpty()) return null;
  8. else if (results.size() == 1) return (Product) results.get(0);
  9. throw new NonUniqueResultException();

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

  1. @Override
  2. public StructuredContentType findStructuredContentTypeByName(String name) {
  3. Query query = em.createNamedQuery("BC_READ_STRUCTURED_CONTENT_TYPE_BY_NAME");
  4. query.setParameter("name",name);
  5. query.setHint(QueryHints.HINT_CACHEABLE, true);
  6. List<StructuredContentType> results = query.getResultList();
  7. if (results.size() > 0) {
  8. return results.get(0);
  9. } else {
  10. return null;
  11. }
  12. }

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

  1. @Test
  2. public void testColumnName() {
  3. EntityManager em = getEntityManager();
  4. em.getTransaction().begin();
  5. Query query = em.createNativeQuery(
  6. "select nte_data, data_MOD_different from naming_test_entity_2_versions where nte_id = :nteId");
  7. query.setParameter("nteId", this.id);
  8. List<Object[]> resultList = query.getResultList();
  9. Assert.assertNotNull(resultList);
  10. Assert.assertTrue(resultList.size() > 0);
  11. Object[] result = resultList.get(0);
  12. Assert.assertEquals(result.length, 2);
  13. em.getTransaction().commit();
  14. }
  15. }

代码示例来源:origin: spring-projects/spring-data-examples

  1. @Override
  2. @SuppressWarnings("unchecked")
  3. public List<Contact> findRelatives(Contact contact) {
  4. return entityManager.createQuery("SELECT u FROM User u WHERE u.lastname = :lastname") //
  5. .setParameter("lastname", contact.getLastname()) //
  6. .getResultList();
  7. }
  8. }

代码示例来源: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: kiegroup/jbpm

  1. public int executeUpdateString(String updateString, Map<String, Object> parameters) {
  2. check();
  3. Query query = this.em.createQuery(updateString);
  4. if (parameters != null && !parameters.isEmpty()) {
  5. for (String name : parameters.keySet()) {
  6. query.setParameter(name, parameters.get(name));
  7. }
  8. }
  9. return query.executeUpdate();
  10. }

代码示例来源: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: BroadleafCommerce/BroadleafCommerce

  1. @Override
  2. public void execute() throws Throwable {
  3. Query q = em.createNamedQuery("BC_ORDER_LOCK_RELEASE");
  4. q.setParameter("orderId", order.getId());
  5. q.setParameter("key", getOrderLockKey());
  6. q.setHint(QueryHints.HINT_CACHEABLE, false);
  7. int rowsAffected = q.executeUpdate();
  8. response[0] = rowsAffected == 1;
  9. }

相关文章