本文整理了Java中org.hibernate.Query.setParameter
方法的一些代码示例,展示了Query.setParameter
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.setParameter
方法的具体详情如下:
包路径:org.hibernate.Query
类名称:Query
方法名:setParameter
[英]Bind a value to a JDBC-style query parameter. The Hibernate type of the parameter is first detected via the usage/position in the query and if not sufficient secondly guessed from the class of the given object.
[中]将值绑定到JDBC样式的查询参数。参数的Hibernate类型首先通过查询中的用法/位置进行检测,如果不够,则从给定对象的类中进行二次猜测。
代码示例来源:origin: spring-projects/spring-framework
@Deprecated
@Override
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
public List<?> find(final String queryString, @Nullable final Object... values) throws DataAccessException {
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
org.hibernate.Query queryObject = queryObject(
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryObject.setParameter(i, values[i]);
}
}
return queryObject.list();
}));
}
代码示例来源:origin: citerus/dddsample-core
@Override
public HandlingHistory lookupHandlingHistoryOfCargo(final TrackingId trackingId) {
return new HandlingHistory(getSession().createQuery(
"from HandlingEvent where cargo.trackingId = :tid").
setParameter("tid", trackingId).
list()
);
}
代码示例来源:origin: citerus/dddsample-core
public Cargo find(TrackingId tid) {
return (Cargo) getSession().
createQuery("from Cargo where trackingId = :tid").
setParameter("tid", tid).
uniqueResult();
}
代码示例来源:origin: hibernate/hibernate-orm
s.save( bar2 );
List list = s.createQuery(
"from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar %'"
).list();
assertTrue( row instanceof Object[] && ( (Object[]) row ).length==3 );
Query q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar%'");
list = q.list();
if ( !(getDialect() instanceof SAPDBDialect) ) assertTrue( list.size()==2 );
q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where ( bar.name in (:nameList) or bar.name in (:nameList) ) and bar.string = :stringVal");
HashSet nameList = new HashSet();
nameList.add( "bar" );
nameList.add( "Bar Two" );
q.setParameterList( "nameList", nameList );
q.setParameter( "stringVal", "a string" );
list = q.list();
if ( !(getDialect() instanceof SAPDBDialect) ) assertTrue( list.size()==2 );
q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like :name and b.name like :name");
q.setString( "name", "Bar%" );
list = q.list();
assertTrue( list.size()==1 );
代码示例来源:origin: bill1012/AdminEAP
public int executeHql(String hql,Map<String,Object> params){
Query query = this.getCurrentSession().createQuery(hql);
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
Object obj = params.get(key);
if (obj instanceof Collection<?>) {
query.setParameterList(key, (Collection<?>) obj);
} else if (obj instanceof Object[]) {
query.setParameterList(key, (Object[]) obj);
} else {
query.setParameter(key, obj);
}
}
}
return query.executeUpdate();
}
代码示例来源:origin: gocd/gocd
public List<Modification> modificationFor(final StageIdentifier stageIdentifier) {
if (stageIdentifier == null) {
return null;
}
String key = cacheKeyForModificationsForStageLocator(stageIdentifier);
List<Modification> modifications = (List<Modification>) goCache.get(key);
if (modifications == null) {
synchronized (key) {
modifications = (List<Modification>) goCache.get(key);
if (modifications == null) {
modifications = (List<Modification>) getHibernateTemplate().execute((HibernateCallback) session -> {
Query q = session.createQuery("FROM Modification WHERE revision = :revision ORDER BY id DESC");
q.setParameter("revision", stageIdentifier.getStageLocator());
return q.list();
});
if (!modifications.isEmpty()) {
goCache.put(key, modifications);
}
}
}
}
return modifications;
}
代码示例来源:origin: netgloo/spring-boot-samples
public User getByEmail(String email) {
return (User) getSession().createQuery(
"from User where email = :email")
.setParameter("email", email)
.uniqueResult();
}
代码示例来源:origin: hibernate/hibernate-orm
s.save(simple );
s.createQuery( "from Simple s where repeat('foo', 3) = 'foofoofoo'" ).list();
s.createQuery( "from Simple s where repeat(s.name, 3) = 'foofoofoo'" ).list();
s.createQuery( "from Simple s where repeat( lower(s.name), (3 + (1-1)) / 2) = 'foofoofoo'" ).list();
assertTrue( q.iterate().hasNext() );
q = s.createQuery("from Simple s where s.name = :foo and upper(s.name) = :bar or s.count=:count or s.count=:count + 1");
q.setParameter( "bar", "SIMPLE 1" );
q.setString( "foo", "Simple 1" );
q.setInteger("count", 69);
q = s.createQuery("select all s, s.other from Simple s where s = :s");
q.setParameter("s", simple);
assertTrue( q.list().size()==1 );
set.add("foo");
q.setParameterList( "name_list", set );
q.setParameter("count", new Integer(-1) );
assertTrue( q.list().size()==1 );
代码示例来源:origin: spring-projects/spring-framework
@Deprecated
@Override
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
public List<?> findByNamedQuery(final String queryName, @Nullable final Object... values) throws DataAccessException {
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
org.hibernate.Query queryObject = queryObject(
ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryObject.setParameter(i, values[i]);
}
}
return queryObject.list();
}));
}
代码示例来源:origin: openmrs/openmrs-core
Query query = sessionFactory.getCurrentSession().createQuery(hql.toString());
query.setParameter("DAEMON_USER_UUID", Daemon.getDaemonUserUuid());
for (Map.Entry<String, String> e : namesMap.entrySet()) {
query.setString(e.getKey(), e.getValue());
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testBetweenParameters() {
final Session s = openSession();
s.getTransaction().begin();
final Query query = s.createQuery( "from Item where quantity between :low and :high" );
query.setParameter( "low", new Integer( 9 ) );
query.setParameter( "high", new Integer( 11 ) );
@SuppressWarnings("unchecked")
final List<Item> result = query.list();
assertEquals( 1, result.size() );
assertEquals( 10, result.get( 0 ).getQuantity().intValue() );
s.getTransaction().commit();
s.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testInsertWithMultipleNamedParams() {
TestData data = new TestData();
data.prepare();
Session s = openSession();
Transaction t = s.beginTransaction();
org.hibernate.Query q = s.createQuery( "insert into Pickup (id, owner, vin) select :id, owner, :vin from Car" );
q.setParameter("id", 5l);
q.setParameter("vin", "some");
q.executeUpdate();
t.commit();
t = s.beginTransaction();
s.createQuery( "delete Vehicle" ).executeUpdate();
t.commit();
s.close();
data.cleanup();
}
代码示例来源:origin: hibernate/hibernate-orm
s.createQuery( "from Simple s where repeat('foo', 3) = 'foofoofoo'" ).list();
s.createQuery( "from Simple s where repeat(s.name, 3) = 'foofoofoo'" ).list();
s.createQuery( "from Simple s where repeat( lower(s.name), 3 + (1-1) / 2) = 'foofoofoo'" ).list();
assertTrue( q.iterate().hasNext() );
q = s.createQuery("from Simple s where s.name = :foo and upper(s.name) = :bar or s.count=:count or s.count=:count + 1");
q.setParameter("bar", "SIMPLE 1");
q.setString("foo", "Simple 1");
q.setInteger("count", 69);
q = s.createQuery("select all s, s.other from Simple s where s = :s");
q.setParameter("s", simple);
assertTrue( q.list().size()==1 );
set.add("Simple 1"); set.add("foo");
q.setParameterList( "name_list", set );
q.setParameter("count", Integer.valueOf( -1 ) );
assertTrue( q.list().size()==1 );
代码示例来源:origin: org.springframework/spring-orm
@Deprecated
@Override
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
public List<?> find(final String queryString, @Nullable final Object... values) throws DataAccessException {
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
org.hibernate.Query queryObject = queryObject(
ReflectionUtils.invokeMethod(createQueryMethod, session, queryString));
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryObject.setParameter(i, values[i]);
}
}
return queryObject.list();
}));
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testRetrievingEntityByZoneDateTime() {
final ZonedDateTime startDate = ZonedDateTime.of(
1,
1,
1,
0,
0,
0,
0,
ZoneOffset.ofHours( 3 )
);
saveZoneDateTimeEventWithStartDate( startDate );
final Session s = openSession();
try {
Query query = s.createQuery( "from ZonedDateTimeEvent o where o.startDate = :date" );
query.setParameter( "date", startDate, ZonedDateTimeType.INSTANCE );
List<ZonedDateTimeEvent> list = query.list();
assertThat( list.size(), is( 1 ) );
}
finally {
s.close();
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testSimpleInsertWithNamedParam() {
TestData data = new TestData();
data.prepare();
Session s = openSession();
Transaction t = s.beginTransaction();
org.hibernate.Query q = s.createQuery( "insert into Pickup (id, owner, vin) select id, :owner, vin from Car" );
q.setParameter("owner", "owner");
q.executeUpdate();
t.commit();
t = s.beginTransaction();
s.createQuery( "delete Vehicle" ).executeUpdate();
t.commit();
s.close();
data.cleanup();
}
代码示例来源:origin: hibernate/hibernate-orm
assertTrue(
"query",
s.createQuery(
"from Detail d, Master m where m = d.master and size(m.outgoing) = 0 and size(m.incoming) = 0"
).list().size()==2
assertTrue( s.createQuery( "select elements(master.details) from Master master" ).list().size()==2 );
t.commit();
s.close();
List list = s.createQuery( "from Master m left join fetch m.details" ).list();
Master m = (Master) list.get(0);
assertTrue( Hibernate.isInitialized( m.getDetails() ) );
master = dd.getMaster();
assertTrue( "detail-master", master.getDetails().contains(dd) );
assertTrue( s.createFilter( master.getDetails(), "order by this.i desc" ).list().size()==2 );
assertTrue( s.createFilter( master.getDetails(), "select this where this.id > -1" ).list().size()==2 );
Query q = s.createFilter( master.getDetails(), "where this.id > :id" );
q.setInteger("id", -1);
assertTrue( q.list().size()==2 );
q = s.createFilter( master.getDetails(), "where this.id > :id1 and this.id < :id2" );
q.setInteger("id1", -1);
f.setParameter( "listy2", master.getName() );
assertEquals( f.iterate().next(), new Integer(12) );
代码示例来源:origin: org.springframework/spring-orm
@Deprecated
@Override
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
public List<?> findByNamedQuery(final String queryName, @Nullable final Object... values) throws DataAccessException {
return nonNull(executeWithNativeSession((HibernateCallback<List<?>>) session -> {
org.hibernate.Query queryObject = queryObject(
ReflectionUtils.invokeMethod(getNamedQueryMethod, session, queryName));
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryObject.setParameter(i, values[i]);
}
}
return queryObject.list();
}));
}
代码示例来源:origin: hibernate/hibernate-orm
Session s = openSession();
try {
Query q = s.createQuery("select bar from Bar as bar where bar.x > :myX");
q.list();
fail("Should throw QueryException for missing myX");
Query q = s.createQuery("select bar from Bar as bar where bar.x > ?");
q.list();
fail("Should throw QueryException for missing ?");
Query q = s.createQuery("select bar from Bar as bar where bar.x > ? or bar.short = 1 or bar.string = 'ff ? bb'");
q.setInteger(0, 1);
q.list();
try {
Query q = s.createQuery("select bar from Bar as bar where bar.string = ? or bar.string = ? or bar.string = ?");
q.setParameter(0, "bull");
q.setParameter(2, "shit");
q.list();
fail("should throw exception telling me i have not set parameter 1");
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testInsertWithSubqueriesAndNamedParams() {
TestData data = new TestData();
data.prepare();
Session s = openSession();
Transaction t = s.beginTransaction();
org.hibernate.Query q = s.createQuery( "insert into Pickup (id, owner, vin) select :id, (select a.description from Animal a where a.description = :description), :vin from Car" );
q.setParameter("id", 5l);
q.setParameter("description", "Frog");
q.setParameter("vin", "some");
q.executeUpdate();
t.commit();
t = s.beginTransaction();
try {
org.hibernate.Query q1 = s.createQuery( "insert into Pickup (id, owner, vin) select :id, (select :description from Animal a where a.description = :description), :vin from Car" );
fail("Unsupported exception should have been thrown");
}
catch (IllegalArgumentException e) {
assertTyping( QueryException.class, e.getCause() );
}
catch(QueryException e) {
assertTrue(e.getMessage().indexOf("Use of parameters in subqueries of INSERT INTO DML statements is not supported.") > -1);
}
t.commit();
t = s.beginTransaction();
s.createQuery( "delete Vehicle" ).executeUpdate();
t.commit();
s.close();
data.cleanup();
}
内容来源于网络,如有侵权,请联系作者删除!