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

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

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

Query.setString介绍

[英]Bind a positional String-valued parameter.
[中]绑定位置字符串值参数。

代码示例

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

@Test
public void testSimpleCaseStatementWithParamResult() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  s.createQuery( "select case p.name when 'Steve' then :opt1 else p.name end from Person p" )
      .setString( "opt1", "x" )
      .list();
  t.commit();
  s.close();
}

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

@Test
public void testSearchedCaseStatementWithParamResult() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  s.createQuery( "select case when p.name = 'Steve' then :opt1 else p.name end from Person p" )
      .setString( "opt1", "x" )
      .list();
  t.commit();
  s.close();
}

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

@Test
public void testDeleteOnMappedJoin() {
  TestData data = new TestData();
  data.prepare();
  Session s = openSession();
  Transaction t = s.beginTransaction();
  int count = s.createQuery( "delete Joiner where joinedName = :joinedName" ).setString( "joinedName", "joined-name" ).executeUpdate();
  assertEquals( "Incorrect deletion count on joined subclass", 1, count );
  t.commit();
  s.close();
  data.cleanup();
}

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

@Test
public void testDeleteParentWithBidirOrphanDeleteCollectionBasedOnPropertyRef() {
  Session session = openSession();
  Transaction txn = session.beginTransaction();
  User user = new User( "test" );
  user.addMail( "test" );
  user.addMail( "test" );
  session.save( user );
  txn.commit();
  session.close();
  session = openSession();
  txn = session.beginTransaction();
  user = ( User ) session.load( User.class, user.getId() );
  session.delete( user );
  txn.commit();
  session.close();
  session = openSession();
  txn = session.beginTransaction();
  session.createQuery( "delete from Mail where alias = :alias" ).setString( "alias", "test" ).executeUpdate();
  session.createQuery( "delete from User where userid = :userid" ).setString( "userid", "test" ).executeUpdate();
  txn.commit();
  session.close();
}

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

@Test
public void testOrdinalParameters() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  s.createQuery( "from Animal a where a.description = ?1 and a.bodyWeight = ?2" )
      .setString( 1, "something" )
      .setFloat( 2, 123f )
      .list();
  s.createQuery( "from Animal a where a.bodyWeight in (?1, ?2)" )
      .setFloat( 1, 999f )
      .setFloat( 2, 123f )
      .list();
  t.commit();
  s.close();
}

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

@Test
public void testDeleteUnionSubclassConcreteSubclass() {
  TestData data = new TestData();
  data.prepare();
  // These should only affect the given table
  Session s = openSession();
  Transaction t = s.beginTransaction();
  int count = s.createQuery( "delete Truck where owner = :owner" ).setString( "owner", "Steve" ).executeUpdate();
  assertEquals( "incorrect restricted update count", 1, count );
  count = s.createQuery( "delete Truck" ).executeUpdate();
  assertEquals( "incorrect update count", 2, count );
  t.commit();
  s.close();
  data.cleanup();
}

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

@Test
public void testDeleteUnionSubclassAbstractRoot() {
  TestData data = new TestData();
  data.prepare();
  // These should reach out into *all* subclass tables...
  Session s = openSession();
  Transaction t = s.beginTransaction();
  int count = s.createQuery( "delete Vehicle where owner = :owner" ).setString( "owner", "Steve" ).executeUpdate();
  assertEquals( "incorrect restricted update count", 1, count );
  count = s.createQuery( "delete Vehicle" ).executeUpdate();
  assertEquals( "incorrect update count", 3, count );
  t.commit();
  s.close();
  data.cleanup();
}

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

@Test
public void testDeleteUnionSubclassLeafSubclass() {
  TestData data = new TestData();
  data.prepare();
  // These should only affect the given table
  Session s = openSession();
  Transaction t = s.beginTransaction();
  int count = s.createQuery( "delete Car where owner = :owner" ).setString( "owner", "Kirsten" ).executeUpdate();
  assertEquals( "incorrect restricted update count", 1, count );
  count = s.createQuery( "delete Car" ).executeUpdate();
  assertEquals( "incorrect update count", 0, count );
  t.commit();
  s.close();
  data.cleanup();
}

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

@Test
public void testFilteredJoinedSubclassHqlUpdateNonLeaf() {
  Session s = openSession();
  s.beginTransaction();
  s.save( new Employee( "John", 'M', "john", new Date() ) );
  s.save( new Employee( "Jane", 'F', "jane", new Date() ) );
  s.save( new Customer( "Charlie", 'M', "charlie", "Acme" ) );
  s.save( new Customer( "Wanda", 'F', "wanda", "ABC" ) );
  s.getTransaction().commit();
  s.close();
  s = openSession();
  s.beginTransaction();
  s.enableFilter( "sex" ).setParameter( "sexCode", Character.valueOf( 'M' ) );
  int count = s.createQuery( "update User u set u.username = :un where u.name = :n" )
      .setString( "un", "charlie" )
      .setString( "n", "Wanda" )
      .executeUpdate();
  assertEquals( 0, count );
  s.getTransaction().commit();
  s.close();
  s = openSession();
  s.beginTransaction();
  s.createQuery( "delete Person" ).executeUpdate();
  s.getTransaction().commit();
  s.close();
}

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

@Test
@SkipForDialect(
    value = CUBRIDDialect.class,
    comment = "As of version 8.4.1 CUBRID does not support temporary tables." +
        " This test somehow calls MultiTableDeleteExecutor which raises an" +
        " exception saying 'cannot doAfterTransactionCompletion multi-table" +
        " deletes using dialect not supporting temp tables'."
)
public void testParameterMixing() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  s.createQuery( "from Animal a where a.description = ?1 and a.bodyWeight = ?2 or a.bodyWeight = :bw" )
      .setString( 1, "something" )
      .setFloat( 2, 12345f )
      .setFloat( "bw", 123f )
      .list();
  t.commit();
  s.close();
}

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

@Test
public void testUpdateOnComponent() {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  Human human = new Human();
  human.setName( new Name( "Stevee", 'X', "Ebersole" ) );
  s.save( human );
  s.flush();
  t.commit();
  String correctName = "Steve";
  t = s.beginTransaction();
  int count = s.createQuery( "update Human set name.first = :correction where id = :id" )
      .setString( "correction", correctName )
      .setLong( "id", human.getId().longValue() )
      .executeUpdate();
  assertEquals( "Incorrect update count", 1, count );
  t.commit();
  t = s.beginTransaction();
  s.refresh( human );
  assertEquals( "Update did not execute properly", correctName, human.getName().getFirst() );
  s.createQuery( "delete Human" ).executeUpdate();
  t.commit();
  s.close();
}

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

@Test
public void testAttachedChildInMerge() {
  fillInitialData();
  Session s = openSession();
  s.beginTransaction();
  Route route = (Route) s.createQuery("FROM Route WHERE name = :name").setString("name", "Route 1").uniqueResult();
  Node n2 = (Node) s.createQuery("FROM Node WHERE name = :name").setString("name", "Node 2").uniqueResult();
  Node n3 = (Node) s.createQuery("FROM Node WHERE name = :name").setString("name", "Node 3").uniqueResult();
  Vehicle vehicle = new Vehicle();
  vehicle.setName("Bus");
  vehicle.setRoute(route);
  Transport $2to3 = new Transport();
  $2to3.setName("Transport 2 -> 3");
  $2to3.setPickupNode(n2); n2.getPickupTransports().add($2to3);
  $2to3.setDeliveryNode(n3); n3.getDeliveryTransports().add($2to3);
  $2to3.setVehicle(vehicle);
  vehicle.setTransports(new HashSet<Transport>(Arrays.asList($2to3)));
  // Try to save graph of transient entities (vehicle, transport) which contains attached entities (node2, node3)
  Vehicle managedVehicle = (Vehicle) s.merge(vehicle);
  checkNewVehicle(managedVehicle);
  s.flush();
  s.clear();
  assertEquals(3, s.createQuery("FROM Transport").list().size());
  assertEquals(2, s.createQuery("FROM Vehicle").list().size());
  assertEquals(4, s.createQuery("FROM Node").list().size());
  Vehicle newVehicle = (Vehicle) s.createQuery("FROM Vehicle WHERE name = :name").setParameter("name", "Bus").uniqueResult();
  checkNewVehicle(newVehicle);
  s.getTransaction().commit();
  s.close();
}

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

@Test
@RequiresDialectFeature(
    value = DialectChecks.SupportsResultSetPositioningOnForwardOnlyCursorCheck.class,
    comment = "Driver does not support result set positioning  methods on forward-only cursors"
)
public void testScrollingJoinFetchesForward() {
  TestData data = new TestData();
  data.prepare();
  Session s = openSession();
  Transaction txn = s.beginTransaction();
  ScrollableResults results = s
      .createQuery( "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
      .setString( "desc", "root%" )
      .scroll( ScrollMode.FORWARD_ONLY );
  int counter = 0;
  while ( results.next() ) {
    counter++;
    Animal animal = ( Animal ) results.get( 0 );
    checkResult( animal );
  }
  assertEquals( "unexpected result count", 2, counter );
  txn.commit();
  s.close();
  data.cleanup();
}

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

Transaction t = s.beginTransaction();
int count = s.createQuery( "update Animal set description = description where description = :desc" )
    .setString( "desc", data.frog.getDescription() )
    .executeUpdate();
assertEquals( "Incorrect entity-updated count", 1, count );
    .setString( "desc", data.polliwog.getDescription() )
    .setString( "newDesc", "Tadpole" )
    .executeUpdate();
assertEquals( "Incorrect entity-updated count", 1, count );

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

@Test
@SkipForDialect(value = CUBRIDDialect.class, comment = "As of verion 8.4.1 CUBRID doesn't support temporary tables. This test fails with"
    + "HibernateException: cannot doAfterTransactionCompletion multi-table deletes using dialect not supporting temp tables")
@SkipForDialect(value = AbstractHANADialect.class, comment = "HANA only supports forward-only cursors.")
public void testScrollingJoinFetchesReverse() {
  TestData data = new TestData();
  data.prepare();
  Session s = openSession();
  Transaction txn = s.beginTransaction();
   ScrollableResults results = s
        .createQuery(
            "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
        .setString( "desc", "root%" ).scroll();
  results.afterLast();
  int counter = 0;
  while ( results.previous() ) {
    counter++;
    Animal animal = ( Animal ) results.get( 0 );
    checkResult( animal );
  }
  assertEquals( "unexpected result count", 2, counter );
  txn.commit();
  s.close();
  data.cleanup();
}

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

@Test
public void testLoading() throws Exception {
  initData();
  Session s = openSession();
  Transaction t = s.beginTransaction();
  Widget obj = (Widget) s.createQuery("from Widget o where o.string = :string").setString("string", "all-normal").uniqueResult();
  assertEquals("Non-Default value incorrectly loaded", obj.getValueOne(), 7);
  assertEquals("Non-Default value incorrectly loaded", obj.getValueTwo(), 8);
  assertEquals("Non-Default value incorrectly loaded", obj.getValueThree(), 9);
  assertEquals("Non-Default value incorrectly loaded", obj.getValueFour(), 10);
  obj = (Widget) s.createQuery("from Widget o where o.string = :string").setString("string", "all-default").uniqueResult();
  assertEquals("Default value incorrectly loaded", obj.getValueOne(), 1);
  assertEquals("Default value incorrectly loaded", obj.getValueTwo(), 2);
  assertEquals("Default value incorrectly loaded", obj.getValueThree(), -1);
  assertEquals("Default value incorrectly loaded", obj.getValueFour(), -5);
  
  t.commit();
  s.close();
  deleteData();
}

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

@Test
public void testManyToAnyWithMap() throws Exception {
  Session s = openSession();
  Transaction t = s.beginTransaction();
  PropertyMap map = new PropertyMap( "sample" );
  map.getProperties().put( "name", new StringProperty( "name", "Alex" ) );
  map.getProperties().put( "age", new IntegerProperty( "age", 33 ) );
  s.save( map );
  s.flush();
  s.clear();
  Query q = s
      .createQuery( "SELECT map FROM PropertyMap map WHERE map.name = :name" );
  q.setString( "name", "sample" );
  PropertyMap actualMap = (PropertyMap) q.uniqueResult();
  assertNotNull( actualMap );
  assertNotNull( actualMap.getProperties() );
  Property property = actualMap.getProperties().get( "name" );
  assertNotNull( property );
  assertTrue( property instanceof StringProperty );
  assertEquals( "Alex", property.asString() );
  property = actualMap.getProperties().get( "age" );
  assertNotNull( property );
  assertTrue( property instanceof IntegerProperty );
  assertEquals( "33", property.asString() );
  t.rollback();
  s.close();
}

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

s.createQuery( "select case when p.name = 'Steve' then :opt1 else :opt2 end from Person p" )
      .setString( "opt1", "x" )
      .setString( "opt2", "y" )
      .list();
  fail( "was expecting an exception" );
s.createQuery( "select case when p.name = 'Steve' then cast( :opt1 as string) else :opt2 end from Person p" )
    .setString( "opt1", "x" )
    .setString( "opt2", "y" )
    .list();

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

q.setString( "name", "sample" );
PropertyList<Property> actualList = (PropertyList<Property>) q
    .uniqueResult();

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

.setString( "desc", "root%" )
.scroll();

相关文章