/** * 在复杂条件中使用REF字段,并且测试在对一情况下,FilterField自动转换为RefField. * * @throws SQLException */ @Test public void testComplextRefCondition() throws SQLException { System.out.println("=========== testComplextRefCondition Start =========="); Person q = new Person(); q.getQuery().addCondition(Person.Field.age, Operator.GREAT, 16); Or or = new Or(); or.addCondition(new RefField(School.Field.id), 2); or.addCondition(new RefField(School.Field.name), Operator.MATCH_ANY, "国"); q.getQuery().addCondition(or); // 凡是引用一个其他表的条件要用RefField包裹 q.getQuery().addCascadeCondition(QB.eq(School.Field.name, "战国高校")); List<Person> result = db.select(q); assertEquals(2, result.size()); System.out.println("=========== testComplextRefCondition End =========="); }
/** * 将多个对象的条件刚合并在一个Query对象中 * * @throws SQLException */ @Test public void testConditionInOneObj() throws SQLException { System.out.println("=========== testConditionInOneObj Begin =========="); long count = db.getSqlTemplate(null) .countBySql("select count(*) from person_table where age>16 and schoolId=2"); assertEquals(2, (int) count); Person q = new Person(); q.getQuery().addCondition(Person.Field.age, Operator.GREAT, 16); q.getQuery().addCondition(School.Field.id, 2); // 凡是引用一个其他表的条件要用RefField包裹 q.getQuery().setCascadeViaOuterJoin(true); List<Person> result = db.select(q); checkResult1(result); System.out.println("=========== testConditionInOneObj End =========="); }
/** * 测试FBI Field,使用JPQL表达式 * * @throws SQLException */ @Test public void testExpression2() throws SQLException { System.out.println("=========== testExpression2 Start =========="); Person p = new Person(); p.getQuery().addCondition(new FBIField("upper(cell)||str(age)", p), "135GG87622"); List<Person> ps = db.select(p); assertTrue(ps.size() > 0); System.out.println("=========== testExpression2 End =========="); }
@Test public void testSelect11() throws SQLException { Person p = new Person(); p.setId(1); p.getQuery().setCascade(false); List<Person> map = db.select(p); LogUtil.show(map); }
/** * 在一对多的关联中使用对子表的过滤条件 * * @throws SQLException */ @Test public void testFilterInOneToManyRef() throws SQLException { ORMConfig.getInstance().setCacheDebug(true); System.out.println("=========== testFilterInOneToManyRef Start =========="); // 无过滤条件的 Person p1 = new Person(); p1.getQuery().addCondition(Person.Field.id, 1); Person result = db.load(p1); assertEquals(6, result.getScores().size()); // 添加过滤条件的 Person p = new Person(); p.getQuery().addCondition(Person.Field.id, 1); p.getQuery().addCascadeCondition(QB.in(Score.Field.subject, new String[] {"语文", "化学", "英语"})); result = db.load(p); assertEquals(3, result.getScores().size()); System.out.println("=========== testFilterInOneToManyRef End =========="); }
/** * 测试使用复杂的子查询过滤条件 * * @throws SQLException */ @Test public void testComplextFilterCondition() throws SQLException { System.out.println("=========== testComplextFilterCondition End =========="); // // 添加过滤条件的 Person p = new Person(); p.getQuery().addCondition(Person.Field.id, 1); Condition or = QB.or( QB.eq(Score.Field.subject, "语文"), QB.eq(Score.Field.subject, "化学"), QB.eq(Score.Field.subject, "英语")); p.getQuery().addCascadeCondition("scores", or); Person result = db.load(p); System.out.println("loaded"); assertEquals(3, result.getScores().size()); System.out.println("=========== testComplextFilterCondition End =========="); }
/** * 当分属多个对象的条件之间要实现Or,And等复合关系时: * * @throws SQLException */ @Test public void testOrAndConditionWithDiffQueryObj() throws SQLException { System.out.println("=========== testOrAndConditionWithDiffQueryObj Begin =========="); Person q = new Person(); And and = new And(); and.addCondition(Person.Field.age, Operator.GREAT, 6); and.addCondition(new RefField(School.Field.id), 1); Or or = new Or(); or.addCondition(and); or.addCondition(Person.Field.age, Operator.GREAT_EQUALS, 99); q.getQuery().addCondition(or); List<Person> result = db.select(q); for (Person p : result) { printPerson(p); } System.out.println("=========== testOrAndConditionWithDiffQueryObj End =========="); }