javax.persistence.EntityManager.createNamedQuery()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(389)

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

EntityManager.createNamedQuery介绍

[英]Create an instance of Query for executing a named query (in the Java Persistence query language or in native SQL).
[中]创建Query的实例以执行命名查询(使用Java持久性查询语言或本机SQL)。

代码示例

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

@Override
public Sku readFirstSku() {
  TypedQuery<Sku> query = em.createNamedQuery("BC_READ_FIRST_SKU", Sku.class);
  return query.getSingleResult();
}

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

@Stateless
public class CommentService {

  @PersistenceContext(unitName = "pu")
  private EntityManager em;

  public void create(Comment t) {
    em.merge(t);
  }

  public Collection<Comment> getAll() {
    Query q = em.createNamedQuery("Comment.findAll");
    Collection<Comment> entities = q.getResultList();
    return entities;
  }
}

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

@Override
  public List<AdminUser> readAdminUserByEmail(String emailAddress) {
    TypedQuery<AdminUser> query = em.createNamedQuery("BC_READ_ADMIN_USER_BY_EMAIL", AdminUser.class);
    query.setParameter("email", emailAddress);
    return query.getResultList();
  }
}

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

@SuppressWarnings("unchecked")
public List<CustomerPhone> readAllCustomerPhonesByCustomerId(Long customerId) {
  Query query = em.createNamedQuery("BC_READ_ALL_CUSTOMER_PHONES_BY_CUSTOMER_ID");
  query.setParameter("customerId", customerId);
  return query.getResultList();
}

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

@Test
public void test_jpql_api_hibernate_named_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    //tag::jpql-api-hibernate-named-query-example[]
    Phone phone = entityManager
      .createNamedQuery( "get_phone_by_number", Phone.class )
      .setParameter( "number", "123-456-7890" )
      .getSingleResult();
    //end::jpql-api-hibernate-named-query-example[]
    assertNotNull( phone );
  });
}

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

List<BlogPost> allPosts = q.getResultList();
Assert.assertNotNull(allPosts);
Assert.assertFalse(allPosts.isEmpty());
q.setParameter("postId", 1);
allPosts = q.getResultList();
Assert.assertNotNull(allPosts);
Assert.assertFalse(allPosts.isEmpty());
q.setParameter("body", "Kundera - Knight in the shining armor!");
allPosts = q.getResultList();
Assert.assertNotNull(allPosts);
Assert.assertFalse(allPosts.isEmpty());
q.setParameter("body", p1.getBody());
q.setParameter("tags", p1.getTags());
q.setParameter("likedBy", p1.getLikedBy());
q.setParameter("comments", p1.getComments());
int updatedRecords = q.executeUpdate();
q = em.createNamedQuery("select.post.2");
allPosts = q.getResultList();
Assert.assertNotNull(allPosts);
int deleteCount = q.executeUpdate();
Assert.assertEquals(2, deleteCount);

代码示例来源: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);
q.setParameter("timeout", orderLockTimeToLive==-1L?orderLockTimeToLive:System.currentTimeMillis() - orderLockTimeToLive);
q.setHint(QueryHints.HINT_CACHEABLE, false);
int rowsAffected = q.executeUpdate();

代码示例来源:origin: javaee-samples/javaee7-samples

@GET
  @Produces("application/xml")
  public Employee[] get() {
    return em.createNamedQuery("Employee.findAll", Employee.class).getResultList().toArray(new Employee[0]);
  }
}

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

@Override
public List<OrderMultishipOption> readOrderItemOrderMultishipOptions(final Long orderItemId) {
  TypedQuery<OrderMultishipOption> query = em.createNamedQuery("BC_READ_MULTISHIP_OPTIONS_BY_ORDER_ITEM_ID", OrderMultishipOption.class);
  query.setParameter("orderItemId", orderItemId);
  return query.getResultList();
}

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

@Override
public void deleteSaga(Class<?> sagaType, String sagaIdentifier, Set<AssociationValue> associationValues) {
  EntityManager entityManager = entityManagerProvider.getEntityManager();
  try {
    entityManager.createNamedQuery(DELETE_ASSOCIATIONS_NAMED_QUERY)
           .setParameter("sagaId", sagaIdentifier)
           .executeUpdate();
    entityManager.createNamedQuery(DELETE_SAGA_NAMED_QUERY)
           .setParameter("id", sagaIdentifier)
           .executeUpdate();
  } catch (EntityNotFoundException e) {
    logger.info("Could not delete {} {}, it appears to have already been deleted.",
          sagaEntryEntityName(),
          sagaIdentifier);
  }
  if (useExplicitFlush) {
    entityManager.flush();
  }
}

代码示例来源:origin: IQSS/dataverse

public ApiToken findApiToken(String token) {
  try {
    return em.createNamedQuery("ApiToken.findByTokenString", ApiToken.class)
        .setParameter("tokenString", token)
        .getSingleResult();
  } catch (NoResultException ex) {
    return null;
  }
}

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

List<BlogPost> allPosts = q.getResultList();
Assert.assertNotNull(allPosts);
Assert.assertFalse(allPosts.isEmpty());
q.setParameter("postId", 1);
allPosts = q.getResultList();
Assert.assertNotNull(allPosts);
Assert.assertFalse(allPosts.isEmpty());
q.setParameter("body", "Kundera - Knight in the shining armor!");
allPosts = q.getResultList();
Assert.assertNotNull(allPosts);
Assert.assertFalse(allPosts.isEmpty());
q.setParameter("body", p1.getBody());
q.setParameter("tags", p1.getTags());
q.setParameter("likedBy", p1.getLikedBy());
q.setParameter("comments", p1.getComments());
int updatedRecords = q.executeUpdate();
q = em.createNamedQuery("select.post.2");
allPosts = q.getResultList();
Assert.assertNotNull(allPosts);
int deleteCount = q.executeUpdate();
Assert.assertEquals(2, deleteCount);

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

@Test
public void test_sql_jpa_multiple_scalar_values_dto_named_query_example() {
  doInJPA( this::entityManagerFactory, entityManager -> {
    //tag::sql-jpa-multiple-scalar-values-dto-named-query-example[]
    List<PersonNames> personNames = entityManager.createNamedQuery(
      "find_person_name_and_nickName_dto" )
    .getResultList();
    //end::sql-jpa-multiple-scalar-values-dto-named-query-example[]
    assertEquals(3, personNames.size());
  });
}

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

TypedQuery<SampleEntity> query = em.createNamedQuery(namedQuery, SampleEntity.class);
  query.getSingleResult();

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

@SuppressWarnings("unchecked")
public List<State> findStates(String countryAbbreviation) {
  Query query = em.createNamedQuery("BC_FIND_STATES_BY_COUNTRY_ABBREVIATION");
  query.setParameter("countryAbbreviation", countryAbbreviation);
  query.setHint(QueryHints.HINT_CACHEABLE, true);
  return query.getResultList();
}

代码示例来源: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();
}

相关文章