/** * 针对一个带有级联关系的对象,实现指定选择列的查询 * * @throws SQLException */ @Test public void testAssignSelectColumn() throws SQLException { db.createTable(Person.class); Transaction db = this.db.startTransaction(); Person p = RandomData.newInstance(Person.class); p.setGender("F"); p.setAge(19); db.insert(p); System.out.println("===========testAssignSelectColumn begin=============="); { Query<Person> t1 = QB.create(Person.class); // 只选择指定的列 t1.setCascadeViaOuterJoin(false); Selects select = QB.selectFrom(t1); // select.clearSelectItems(); select.guessColumn("schoolId"); // select.column(School.Field.name); select.guessColumn("schoolName"); select.columns(t1, "name,age,cell"); select.column(t1, "id"); List<Person> result = db.select(t1); LogUtil.show(result.get(0)); } { Query<Person> t1 = QB.create(Person.class); // 只选择指定的列 // t1.setAutoOuterJoin(false); Selects select = QB.selectFrom(t1); // select.clearSelectItems(); select.guessColumn("schoolId"); // select.column(School.Field.name); select.guessColumn("schoolName"); select.columns(t1, "name,age,cell"); select.column(t1, "id"); List<Person> result = db.select(t1); LogUtil.show(result.get(0)); } { Query<Person> t1 = QB.create(Person.class); // 只选择指定的列 // t1.setAutoOuterJoin(false); Selects select = QB.selectFrom(t1); // select.clearSelectItems(); select.guessColumn("schoolId"); // select.column(School.Field.name); select.guessColumn("schoolName"); select.columns(t1, "name,age,cell"); select.column(t1, "id"); t1.setCascade(false); List<Person> result = db.select(t1.getInstance()); LogUtil.show(result.get(0)); } db.rollback(true); System.out.println("===========result=============="); }
@Test public void testDeleteNormal() throws SQLException { Transaction db = this.db.startTransaction(); Person p = new Person(); p.setId(1); db.delete(p); db.rollback(true); }
@Test public void testDeleteCascade() throws SQLException { Transaction db = this.db.startTransaction(); Person p = new Person(); p.setAge(20); db.select(p); System.out.println(p.getScores()); db.deleteCascade(p); db.rollback(true); }
/** * 测试级联的更新操作 * * @throws SQLException */ @Test public void testRefUpdate() throws SQLException { System.out.println("=========== testRefUpdate Begin =========="); Transaction db = this.db.startTransaction(); Person p = new Person(); p.setId(2); p = db.load(p); // p.setSchoolId(0); p.setSchool(new School("华南大学")); p.setAge(123); db.updateCascade(p); db.rollback(true); System.out.println("=========== testRefUpdate End =========="); }
@Test public void testUpdateNormal() throws SQLException { Transaction db = this.db.startTransaction(); List<School> schools = db.selectAll(School.class); School s = schools.get(0); Person p = new Person(); p.setId(2); p = db.load(p); System.out.println(p); System.out.println(p.getSchoolId()); System.out.println(p.getSchool()); p.setSchool(s); db.update(p); db.rollback(true); }
/** * 测试分组 * * @throws SQLException */ @Test public void testGroup() throws SQLException { Transaction db = this.db.startTransaction(); Person p = RandomData.newInstance(Person.class); p.setGender("F"); p.setAge(19); db.insert(p); Query<Person> t1 = QB.create(Person.class); Selects select = QB.selectFrom(t1); select.column(Person.Field.gender).group(); select.column(Person.Field.id).count().as("count"); select.column(Person.Field.age).min().as("minAge"); select.column(Person.Field.age).max().as("maxAge").having(Operator.GREAT, 0); List<Map> map = db.selectAs(t1, Map.class); db.rollback(true); LogUtil.show(map); }
@Test public void testInsertCascade() throws SQLException { Transaction db = this.db.startTransaction(); List<School> schools = db.selectAll(School.class); School s = schools.get(0); Person p = new Person(); p.setAge(12); p.setBirthDay(new Date()); p.setCell("123433454"); p.setFriendComment(new String[] {"AA"}); p.setGender("M"); p.setLastModified(new Date()); p.setParentId(1); p.setSchool(s); p.setName("刘备"); p.setSchoolId(3); db.insertCascade(p); System.out.println(p.getSchoolId()); db.rollback(true); }