org.hibernate.query.Query.setParameter()方法的使用及代码示例

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

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

Query.setParameter介绍

[英]Bind a value to a JDBC-style query parameter.
[中]将值绑定到JDBC样式的查询参数。

代码示例

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

public Query<?> getRevisionsQuery(Session session, Set<Number> revisions) {
    return session.createQuery(
        String.format( REVISIONS_QUERY, revisionInfoEntityName, revisionInfoIdName )
    ).setParameter( REVISIONS_QUERY_PARAMETER, revisions );
  }
}

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

public Query<?> getRevisionDateQuery(Session session, Number revision) {
  return session.createQuery(
      String.format(
          REVISION_DATE_QUERY,
          revisionInfoTimestampName,
          revisionInfoEntityName,
          revisionInfoIdName
      )
  ).setParameter( REVISION_DATE_QUERY_PARAMETER, revision );
}

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

public Query<?> getRevisionNumberForDateQuery(Session session, Date date) {
  return session.createQuery(
      String.format(
          REVISION_NUMBER_FOR_DATE_QUERY,
          revisionInfoIdName,
          revisionInfoEntityName,
          revisionInfoTimestampName
      )
  ).setParameter( REVISION_NUMBER_FOR_DATE_QUERY_PARAMETER, timestampAsDate ? date : date.getTime() );
}

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

public Query toQuery(Session session) {
    final StringBuilder querySb = new StringBuilder();
    final Map<String, Object> queryParamValues = new HashMap<>();

    build( querySb, queryParamValues );

    final Query query = session.createQuery( querySb.toString() );
    for ( Map.Entry<String, Object> paramValue : queryParamValues.entrySet() ) {
      query.setParameter( paramValue.getKey(), paramValue.getValue() );
    }

    return query;
  }
}

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

@Override
public Person findByName(String name) {
  return (Person) this.sessionFactory.getCurrentSession().createQuery(
    "from Person person where person.name = :name").setParameter("name", name).getSingleResult();
}

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

protected int doDelete(Session session, String queryString, Object param, Type paramType) {
  Query query = session.createQuery( queryString )
      .setParameter( 0, param, paramType );
  return doDelete( session, query );
}

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

public Query getQuery(Session s) {
    return s.createQuery( "from Student s where s.studentNumber = :studentNumber" )
        .setParameter( "studentNumber", shermanExpected.getStudentNumber() );
  }
};

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

public Query getQuery(Session s) {
    return s.createQuery(
        "select e.student, e.semester, e.year, e.course from Enrolment e  where e.studentNumber = :studentNumber" )
        .setParameter( "studentNumber", shermanEnrolmentExpected.getStudentNumber() );
  }
};

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

public Query getQuery(Session s) {
    return s.createQuery( "select e.semester from Enrolment e where e.studentNumber = :studentNumber" )
        .setParameter( "studentNumber", shermanEnrolmentExpected.getStudentNumber() );
  }
};

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

@SuppressWarnings("unchecked")
private static void executeAndVerifySelect(Session session) {
  List<Actor> list = session.createQuery("from Actor where firstName = ?0")
      .setParameter(0, "CHRISTIAN").list();
  Set<String> expectedLastNames = new HashSet<>(Arrays.asList("GABLE", "AKROYD", "NEESON"));
  assertEquals(expectedLastNames.size(), list.size());
  for (Actor actor : list) {
    assertTrue(expectedLastNames.remove(actor.getLastName()));
  }
}

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

@Test
public void testUpdate() {
  doInHibernate( this::sessionFactory, session -> {
    int updateCount = session.createQuery( "update Person set name = :name where employed = :employed" )
        .setParameter( "name", "John Doe" )
        .setParameter( "employed", true )
        .executeUpdate();
    assertEquals(entityCount(), updateCount);
  });
}

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

@Test
public void testSetParameter() throws Exception {
  try (Session session = openSession()) {
    final Query<TestEntity> query = session.createQuery(
        "SELECT e FROM TestEntity e WHERE e.date <= :ts",
        TestEntity.class
    ).setParameter( "ts", new DateAttribute( System.currentTimeMillis() ), TemporalType.TIMESTAMP );
    final Stream<TestEntity> stream = query.stream();
    assertThat( stream.count(), is( 1L ) );
  }
}

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

@Test
public void test() {
  doInHibernate( this::sessionFactory, session -> {
    List<CorporateUser> users = session.createQuery(
      "select u from CorporateUser u where u.emailAddresses = :address", CorporateUser.class )
    .setParameter( "address", new Array(), ArrayType.INSTANCE )
    .getResultList();
    assertTrue( users.isEmpty() );
  } );
}

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

@Test
@TestForIssue(jiraKey = "HHH-2735")
public void testQueryLockModePessimisticWriteWithAlias() {
  doInHibernate( this::sessionFactory, session -> {
    // shouldn't throw an exception
    session.createQuery( "SELECT MAX(a.id)+1 FROM A a where a.value = :value" )
        .setLockMode( "a", LockMode.PESSIMISTIC_WRITE )
        .setParameter( "value", "it" )
        .list();
  } );
}

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

@Test
public void testStoredProcedureOutParameter() {
  doInHibernate( this::sessionFactory, session -> {
    List<Object[]> persons = session
        .createNamedQuery(
            "getPerson")
        .setParameter(1, 1L)
        .getResultList();
    assertEquals(1, persons.size());
  } );
}

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

@Test
public void HHH_10463_TestCoalesce() {
  doInHibernate( this::sessionFactory, session -> {
    Query query = session.createQuery( "from Person p where p.name = coalesce(:name , p.name) ");
    query.setParameter("name", "Johannes");
    List<Person> resultList = query.getResultList();
    assertThat(resultList, hasItem(person));
  } );
}

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

@Test
@TestForIssue(jiraKey = "HHH-2735")
public void testQueryLockModeNoneWithAlias() {
  doInHibernate( this::sessionFactory, session -> {
    // shouldn't throw an exception
    session.createQuery( "SELECT a.value FROM A a where a.id = :id" )
        .setLockMode( "a", LockMode.NONE )
        .setParameter( "id", id )
        .list();
  } );
}

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

@Test
public void testDeleteFromPerson() {
  doInHibernate( this::sessionFactory, session -> {
    //tag::batch-bulk-hql-temp-table-delete-query-example[]
    int updateCount = session.createQuery(
      "delete from Person where employed = :employed" )
    .setParameter( "employed", false )
    .executeUpdate();
    //end::batch-bulk-hql-temp-table-delete-query-example[]
    assertEquals( entityCount(), updateCount );
  });
}

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

@Test
public void testDeleteFromEngineer() {
  doInHibernate( this::sessionFactory, session -> {
    int updateCount = session.createQuery( "delete from Engineer where fellow = :fellow" )
        .setParameter( "fellow", true )
        .executeUpdate();
    assertEquals( entityCount() / 2, updateCount );
  });
}

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

@Test
@SkipForDialect( jiraKey = "HHH-10463", value =  PostgreSQL81Dialect.class)
@SkipForDialect( jiraKey = "HHH-10463", value =  Oracle8iDialect.class)
public void HHH_10463_NullInCoalesce() {
  doInHibernate( this::sessionFactory, session -> {
    Query query = session.createQuery("from Person p where p.name = coalesce(:name, p.name) ");
    query.setParameter("name", null);
    List<Person> resultList = query.getResultList();
    assertThat(resultList, hasItem(person));
  } );
}

相关文章