本文整理了Java中javax.persistence.Query.setParameter
方法的一些代码示例,展示了Query.setParameter
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.setParameter
方法的具体详情如下:
包路径:javax.persistence.Query
类名称:Query
方法名:setParameter
[英]Bind an argument value to a positional parameter.
[中]将参数值绑定到位置参数。
代码示例来源:origin: spring-projects/spring-data-examples
@Override
@SuppressWarnings("unchecked")
public List<Employee> findSubordinates(Employee manager) {
return entityManager.createQuery("SELECT u from User u where u.manager = :manager") //
.setParameter("manager", manager) //
.getResultList();
}
}
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
@Override
public CategoryXref readXrefByIds(Long categoryId, Long subCategoryId) {
Query query = em.createNamedQuery("BC_READ_CATEGORY_XREF_BY_IDS");
query.setParameter("categoryId", categoryId);
query.setParameter("subCategoryId", subCategoryId);
return (CategoryXref) query.getSingleResult();
}
代码示例来源: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: shopizer-ecommerce/shopizer
Query q = this.em.createQuery(hql);
q.setParameter("cm", store.getId());
q.setParameter("se", seUrl);
Content content = (Content)q.getSingleResult();
List<Content> results = q.getResultList();
if (results.isEmpty()) {
return null;
} else if (results.size() >= 1) {
content = results.get(0);
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
@SuppressWarnings("unchecked")
public CreditCardPayment findCreditCardPayment(String referenceNumber) {
Query query = em.createNamedQuery("BC_READ_CREDIT_CARD_BY_REFERENCE_NUMBER");
query.setParameter("referenceNumber", referenceNumber);
List<CreditCardPayment> infos = query.getResultList();
CreditCardPayment response = (infos == null || infos.size() == 0) ? null : infos.get(0);
if (response != null) {
response.setEncryptionModule(encryptionModule);
}
return response;
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void test_sql_jpa_query_parameters_example() {
doInJPA( this::entityManagerFactory, entityManager -> {
//tag::sql-jpa-query-parameters-example[]
List<Person> persons = entityManager.createNativeQuery(
"SELECT * " +
"FROM Person " +
"WHERE name like :name", Person.class )
.setParameter("name", "J%")
.getResultList();
//end::sql-jpa-query-parameters-example[]
assertEquals(1, persons.size());
});
}
代码示例来源: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: AxonFramework/AxonFramework
@Override
public void releaseClaim(String processorName, int segment) {
EntityManager entityManager = entityManagerProvider.getEntityManager();
int updates = entityManager.createQuery(
"UPDATE TokenEntry te SET te.owner = null " +
"WHERE te.owner = :owner AND te.processorName = :processorName " +
"AND te.segment = :segment"
).setParameter("processorName", processorName).setParameter("segment", segment).setParameter("owner", nodeId)
.executeUpdate();
if (updates == 0) {
logger.warn("Releasing claim of token {}/{} failed. It was not owned by {}", processorName, segment,
nodeId);
}
}
代码示例来源:origin: kiegroup/jbpm
@Override
public int executeUpdate(String queryName, Map<String, Object> params) {
check();
Query query = this.em.createNamedQuery(queryName);
if (params != null) {
for (Map.Entry<String, Object> paramEntry : params.entrySet()) {
query.setParameter(paramEntry.getKey(), paramEntry.getValue());
}
}
return query.executeUpdate();
}
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
String orderLockKey = getOrderLockKey();
Query q = em.createNamedQuery("BC_ORDER_LOCK_READ");
q.setParameter("orderId", order.getId());
q.setParameter("key", orderLockKey);
q.setHint(QueryHints.HINT_CACHEABLE, false);
Long count = (Long) q.getSingleResult();
q = em.createNamedQuery("BC_ORDER_LOCK_ACQUIRE");
q.setParameter("orderId", order.getId());
q.setParameter("currentTime", System.currentTimeMillis());
q.setParameter("key", orderLockKey);
Long orderLockTimeToLive = getDatabaseOrderLockTimeToLive();
q.setParameter("timeout", orderLockTimeToLive==-1L?orderLockTimeToLive:System.currentTimeMillis() - orderLockTimeToLive);
q.setHint(QueryHints.HINT_CACHEABLE, false);
int rowsAffected = q.executeUpdate();
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
@Override
public ChallengeQuestion readChallengeQuestionById(long challengeQuestionId) {
Query query = em.createNamedQuery("BC_READ_CHALLENGE_QUESTION_BY_ID");
query.setParameter("question_id", challengeQuestionId);
List<ChallengeQuestion> challengeQuestions = query.getResultList();
return challengeQuestions == null || challengeQuestions.isEmpty() ? null : challengeQuestions.get(0);
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void test_jpql_api_positional_parameter_example() {
doInJPA( this::entityManagerFactory, entityManager -> {
//tag::jpql-api-positional-parameter-example[]
Query query = entityManager.createQuery(
"select p " +
"from Person p " +
"where p.name like ?1" )
.setParameter( 1, "J%" );
//end::jpql-api-positional-parameter-example[]
});
}
代码示例来源:origin: shopizer-ecommerce/shopizer
Query q = this.em.createQuery(hql);
q.setParameter("pid", productId);
q.setParameter("lid", regionList);
q.setParameter("dt", new Date());
q.setParameter("lang", language.getId());
List<Product> results = q.getResultList();
if (results.isEmpty()) return null;
else if (results.size() == 1) return (Product) results.get(0);
throw new NonUniqueResultException();
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
@Override
public StructuredContentType findStructuredContentTypeByName(String name) {
Query query = em.createNamedQuery("BC_READ_STRUCTURED_CONTENT_TYPE_BY_NAME");
query.setParameter("name",name);
query.setHint(QueryHints.HINT_CACHEABLE, true);
List<StructuredContentType> results = query.getResultList();
if (results.size() > 0) {
return results.get(0);
} else {
return null;
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testColumnName() {
EntityManager em = getEntityManager();
em.getTransaction().begin();
Query query = em.createNativeQuery(
"select nte_data, data_MOD_different from naming_test_entity_2_versions where nte_id = :nteId");
query.setParameter("nteId", this.id);
List<Object[]> resultList = query.getResultList();
Assert.assertNotNull(resultList);
Assert.assertTrue(resultList.size() > 0);
Object[] result = resultList.get(0);
Assert.assertEquals(result.length, 2);
em.getTransaction().commit();
}
}
代码示例来源:origin: spring-projects/spring-data-examples
@Override
@SuppressWarnings("unchecked")
public List<Contact> findRelatives(Contact contact) {
return entityManager.createQuery("SELECT u FROM User u WHERE u.lastname = :lastname") //
.setParameter("lastname", contact.getLastname()) //
.getResultList();
}
}
代码示例来源: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: kiegroup/jbpm
public int executeUpdateString(String updateString, Map<String, Object> parameters) {
check();
Query query = this.em.createQuery(updateString);
if (parameters != null && !parameters.isEmpty()) {
for (String name : parameters.keySet()) {
query.setParameter(name, parameters.get(name));
}
}
return query.executeUpdate();
}
代码示例来源: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: BroadleafCommerce/BroadleafCommerce
@Override
public void execute() throws Throwable {
Query q = em.createNamedQuery("BC_ORDER_LOCK_RELEASE");
q.setParameter("orderId", order.getId());
q.setParameter("key", getOrderLockKey());
q.setHint(QueryHints.HINT_CACHEABLE, false);
int rowsAffected = q.executeUpdate();
response[0] = rowsAffected == 1;
}
内容来源于网络,如有侵权,请联系作者删除!