KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > OneOneBi


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23 public class OneOneBi extends JormcRunner {
24
25     public final static String JavaDoc PROJECT_NAME = "jormExamples";
26     public final static String JavaDoc A_CLASS_NAME = "org.objectweb.jorm.examples.oob.A";
27     public final static String JavaDoc B_CLASS_NAME = "org.objectweb.jorm.examples.oob.B";
28
29
30     public static void main(String JavaDoc[] args) {
31         try {
32             OneOneBi oob = new OneOneBi(args.length == 0 ? "./" : args[0]);
33             logger.log(BasicLevel.INFO, "Example OneOneBi");
34             oob.createMIofA();
35             oob.createMIofB();
36             oob.generate();
37         } catch (Exception JavaDoc e) {
38             logger.log(BasicLevel.ERROR, "", getInner(e));
39         }
40     }
41     public OneOneBi(String JavaDoc output) throws PException {
42         super(PROJECT_NAME, output);
43     }
44
45     public void createMIofA() throws PException {
46         logger.log(BasicLevel.INFO, "Create the meta information of the A class");
47         Class JavaDoc aclass = compiler.getMIManager().createClass(A_CLASS_NAME);
48
49         PrimitiveElement ida =
50                 aclass.createPrimitiveElement("ida", PTypeSpace.LONG);
51         PrimitiveElement f1 =
52                         aclass.createPrimitiveElement("f1", PTypeSpace.STRING);
53
54         // pk simple
55
NameDef aNameDef = aclass.createNameDef();
56         aNameDef.setFieldName("ida");
57
58         // reference to B
59
ScalarField idb = aclass.createHiddenField("idb", PTypeSpace.LONG);
60         Class JavaDoc bclass = compiler.getMIManager().createClass(B_CLASS_NAME);
61         ClassRef bcr = aclass.createClassRef("myB", bclass);
62         NameDef myBNameDef = bcr.createRefNameDef();
63         myBNameDef.setFieldName(idb.getName());
64
65         // ----------------- Mapping for A -----------------
66
ClassProject acp = aclass.createClassProject(PROJECT_NAME);
67         RdbMapping am = (RdbMapping) acp.createMapping("rdb");
68
69         RdbClassMapping acm = am.createClassMapping("to-table");
70         RdbTable tableA = acm.createRdbTable("table_A");
71         //Identifier of the A class
72
acm.createIdentifierMapping(aNameDef);
73
74         // Mapping rule of the reference myB
75
acm.createReferenceMapping("embedded-target-reference", myBNameDef);
76
77         // Mapping of the ida field over the pka_col
78
tableA.createPrimitiveElementMapping(ida, "pka_col", null, true);
79         // Mapping of the f1 field over the f1_col
80
tableA.createPrimitiveElementMapping(f1, "f1_col", null, true);
81         // Mapping of the idb field over the fkb_col
82
tableA.createPrimitiveElementMapping(idb, "fkb_col", null, false);
83     }
84
85     public void createMIofB() throws PException {
86         logger.log(BasicLevel.INFO, "Create the meta information of the B class");
87         Class JavaDoc bclass = compiler.getMIManager().createClass(B_CLASS_NAME);
88         PrimitiveElement idb =
89                 bclass.createPrimitiveElement("idb", PTypeSpace.LONG);
90         PrimitiveElement f2 =
91                 bclass.createPrimitiveElement("f2", PTypeSpace.STRING);
92
93         // pk simple
94
NameDef bNameDef = bclass.createNameDef();
95         bNameDef.setFieldName(idb.getName());
96
97         // reference to A
98
ScalarField ida = bclass.createHiddenField("ida", PTypeSpace.LONG);
99         Class JavaDoc aclass = compiler.getMIManager().createClass(A_CLASS_NAME);
100         ClassRef acr = bclass.createClassRef("myA", aclass);
101         NameDef myANameDef = acr.createRefNameDef();
102         myANameDef.setFieldName(ida.getName());
103
104         // ----------------- Mapping for B -----------------
105

106         ClassProject bcp = bclass.createClassProject(PROJECT_NAME);
107         RdbMapping bm = (RdbMapping) bcp.createMapping("rdb");
108
109         RdbClassMultiMapping bcm = bm.createClassMultiMapping("to-table");
110         RdbTable tableB = bcm.createRdbTable("table_B");
111         bcm.createIdentifierMapping(bNameDef);
112
113         bcm.createReferenceMapping("multi-table", myANameDef);
114
115         tableB.createPrimitiveElementMapping(idb, "pkb_col", null, true);
116         tableB.createPrimitiveElementMapping(f2, "f2_col", null, true);
117
118         //Mapping of the reference to A
119
RdbExternalTable extTableA = bcm.createRdbExternalTable("table_A");
120         extTableA.setReadOnly(true); // optimisation to avoid writings
121
extTableA.createPrimitiveElementMapping(ida, "pka_col");
122         RdbJoin joinToA = extTableA.createRdbJoin("myA_Join");
123         joinToA.addJoinColumnNames("pkb_col", "fkb_col");
124     }
125 }
126
Popular Tags