source: trunk/fmgVen/test/com/fmguler/ven/sample/Sample.java @ 28

Last change on this file since 28 was 28, checked in by fmguler, 13 years ago

Refs #3 - Ven.get() is converted to the new format. QueryGenerator and QueryMapper are mostly OK, generateRecursively and mapRecursively are converted and checked. Handles joins (includes/associations) many to one and one to many. For one to many, the reverse join field can be determined in a couple of ways. First way is (prefereed) having VenList as the list implementation which specifies the element class and the join field. Second way is using Java 1.5 generic type to detect element class (not yet implemented) and guessing join field by convention (if multiple joins exist, this won't work). The last way is to have some kind of annotation or configuration, which is of course the least preferred way. VenList has a static method to determine the element class in the object list, which currently calls getElementClass if the list is an instance of VenList. In the future other options can be implemented.

Getting object using joins (includes/associations) are tested using dummy assocations between SomeDomainObject and AnotherDomainObject. The Sample class builds the database, tests the operations and rolls back to the initial state. Database refactoring operations are moved to the LiquibaseUtil for clarity.

In the future, the generated queries will be shortened using hashed aliases, and the criteria subsystem will be implemented.

File size: 3.6 KB
Line 
1/*
2 *  fmgVen - A Convention over Configuration Java ORM Tool
3 *  Copyright 2010 Fatih Mehmet Güler
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *  under the License.
17 */
18package com.fmguler.ven.sample;
19
20import com.fmguler.ven.LiquibaseUtil;
21import com.fmguler.ven.Ven;
22import com.fmguler.ven.sample.domain.AnotherDomainObject;
23import com.fmguler.ven.sample.domain.SomeDomainObject;
24import java.util.Date;
25import java.util.HashSet;
26import java.util.List;
27import java.util.Set;
28
29/**
30 * Demonstrates sample usage of fmgVen.
31 * @author Fatih Mehmet Güler
32 */
33public class Sample {
34    public static void main(String[] args) {
35        //build the sample database
36        LiquibaseUtil.buildDatabase();
37
38        //save an object
39        testSave();
40        //get an object
41        testGet();
42        //delete an object
43        testDelete();
44
45        //rollback the sample database to original state
46        LiquibaseUtil.rollbackDatabase("tag-init");
47    }
48
49    /**
50     * Test save an object
51     */
52    public static void testSave() {
53        Ven ven = getVen();
54
55        //insert
56        SomeDomainObject obj = new SomeDomainObject();
57        obj.setName("name");
58        obj.setDescription("desc");
59        obj.setDate(new Date());
60        ven.save(obj);
61        System.out.println(obj);
62
63        //update
64        obj.setName("name update");
65        ven.save(obj);
66        System.out.println(obj);
67    }
68
69    /**
70     * Test delete an object
71     */
72    public static void testDelete() {
73        Ven ven = getVen();
74        ven.delete(2, SomeDomainObject.class);
75    }
76
77    /**
78     * Test get an object by primary key
79     */
80    public static void testGet() {
81        Ven ven = getVen();
82
83        //get with includes
84        Set joins = new HashSet();
85        joins.add("SomeDomainObject.anotherDomainObjects");
86        joins.add("SomeDomainObject.anotherDomainObject");
87        SomeDomainObject obj = (SomeDomainObject)ven.get(1, SomeDomainObject.class, joins);
88        System.out.println(obj);
89
90        Set joins2 = new HashSet();
91        joins2.add("AnotherDomainObject.someDomainObject");
92        AnotherDomainObject obj2 = (AnotherDomainObject)ven.get(1, AnotherDomainObject.class, joins2);
93        System.out.println(obj2);
94    }
95
96    /**
97     * Test list the collection of objects
98     */
99    public static void testList() {
100        Ven ven = getVen();
101        List objList = ven.list(SomeDomainObject.class);
102        System.out.println(objList);
103    }
104
105    /**
106     * Test list the collection of objects by some criteria
107     */
108    public static void testListByCriteria() {
109        Ven ven = getVen();
110        List objList = ven.list(SomeDomainObject.class/*, criteria */);
111        System.out.println(objList);
112    }
113
114    //---------------------------------------------------------
115    private static Ven getVen() {
116        Ven ven = new Ven();
117        ven.setDataSource(LiquibaseUtil.getDataSource());
118        ven.addDomainPackage("com.fmguler.ven.sample.domain").addDomainPackage("another.package");
119        return ven;
120    }
121}
Note: See TracBrowser for help on using the repository browser.