2 Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 package testsuite.clusterj;
20 import java.util.List;
22 import testsuite.clusterj.model.Employee;
24 import com.mysql.clusterj.ClusterJUserException;
25 import com.mysql.clusterj.Query;
27 import com.mysql.clusterj.query.PredicateOperand;
28 import com.mysql.clusterj.query.QueryBuilder;
29 import com.mysql.clusterj.query.QueryDomainType;
31 public class QueryMultipleParameterTest extends AbstractQueryTest {
33 /** The persistent class
34 @PersistenceCapable(table="t_basic")
35 public interface Employee extends IdBase {
42 void setName(String name);
44 @Index(name="idx_unique_hash_magic")
46 void setMagic(int magic);
48 @Index(name="idx_btree_age")
50 void setAge(Integer age);
54 public Class<?> getInstanceType() {
55 return Employee.class;
59 void createInstances(int number) {
60 createEmployeeInstances(10);
61 instances.addAll(employees);
65 // QueryBuilder is the sessionFactory for queries
66 QueryBuilder builder = session.getQueryBuilder();
67 // QueryDomainType is the main interface
68 QueryDomainType<Employee>dobj = builder.createQueryDefinition(Employee.class);
70 PredicateOperand param1 = dobj.param("param1");
71 PredicateOperand param2 = dobj.param("param2");
73 PredicateOperand propertyMagic = dobj.get("magic");
74 PredicateOperand propertyId = dobj.get("id");
76 // param1 is used in two different places but same type (int) in both
77 dobj.where(propertyMagic.equal(param1).and(propertyId.between(param1, param2)));
78 Query<Employee> query = session.createQuery(dobj);
79 query.setParameter("param1", 4);
80 query.setParameter("param2", 5);
81 List<Employee> result = query.getResultList();
82 errorIfNotEqual("Wrong size of result", 1, result.size());
83 if (result.size() == 1) {
84 errorIfNotEqual("Wrong result", 4, result.get(0).getId());
89 public void testNegative() {
91 // QueryBuilder is the sessionFactory for queries
92 QueryBuilder builder = session.getQueryBuilder();
93 // QueryDomainType is the main interface
94 QueryDomainType<Employee>dobj = builder.createQueryDefinition(Employee.class);
96 PredicateOperand param1 = dobj.param("param1");
97 PredicateOperand param2 = dobj.param("param2");
99 PredicateOperand propertyAge = dobj.get("magic");
100 PredicateOperand propertyMagic = dobj.get("name");
102 // expect an exception here because param1 is used for String name and int magic
103 dobj.where(propertyAge.equal(param1).and(propertyMagic.between(param1, param2)));
104 } catch (ClusterJUserException e) {