KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > OneManyBi


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
24 import org.objectweb.util.monolog.api.BasicLevel;
25 import org.objectweb.jorm.api.PException;
26 import org.objectweb.jorm.metainfo.api.Class;
27 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
28 import org.objectweb.jorm.metainfo.api.NameDef;
29 import org.objectweb.jorm.metainfo.api.ScalarField;
30 import org.objectweb.jorm.metainfo.api.ClassRef;
31 import org.objectweb.jorm.metainfo.api.ClassProject;
32 import org.objectweb.jorm.metainfo.api.GenClassRef;
33 import org.objectweb.jorm.metainfo.rdb.api.RdbMapping;
34 import org.objectweb.jorm.metainfo.rdb.api.RdbClassMapping;
35 import org.objectweb.jorm.metainfo.rdb.api.RdbTable;
36 import org.objectweb.jorm.metainfo.rdb.api.RdbClassMultiMapping;
37 import org.objectweb.jorm.metainfo.rdb.api.RdbExternalTable;
38 import org.objectweb.jorm.metainfo.rdb.api.RdbJoin;
39 import org.objectweb.jorm.metainfo.rdb.api.RdbGenClassMapping;
40 import org.objectweb.jorm.type.api.PTypeSpace;
41
42 /**
43  * This example builds the jorm meta information for a multivalued relation
44  * between two persistent object. The A objects has a multivalued reference
45  * 'myB' to a B object, whereas the B has a reference 'myA' to the A object.
46  * This is a one-many relationship.
47  * In a mapping point of view the objects are stored on sql table with the
48  * following schema
49  * <!--
50  * |---------|---------| * |---------|---------|---------|
51  * | pka_col | f1_col | * | pkb_col | f2_col | fka_col |
52  * |---------|---------| * |---------|---------|---------|
53  * | ... | ... | * | ... | ... | ... |
54  * |---------|---------| * |---------|---------|---------|
55  * table_A * table_B
56  * -->
57  * <table border=0>
58  * <TR><TD>table_A</TD><TD>table_B</TD></TR>
59  * <TR>
60  * <TD><TABLE>
61  * <TR><TD>pka_col</TD><TD>f1_col</TD></TR>
62  * <TR><TD>...</TD><TD>...</TD></TR>
63  * </TABLE></TD>
64  * <TD><TABLE>
65  * <TR><TD>pkb_col</TD><TD>f2_col</TD><TD>fka_col</TD></TR>
66  * <TR><TD>...</TD><TD>...</TD><TD>...</TD></TR>
67  * </TABLE></TD>
68  * </TR>
69  * </table>
70  *
71  * S.Chassande-Barrioz
72  */

73
74 public class OneManyBi extends JormcRunner {
75
76     public final static String JavaDoc PROJECT_NAME = "jormExamples";
77     public final static String JavaDoc A_CLASS_NAME = "org.objectweb.jorm.examples.omb.A";
78     public final static String JavaDoc B_CLASS_NAME = "org.objectweb.jorm.examples.omb.B";
79
80
81     public static void main(String JavaDoc[] args) {
82         try {
83             OneManyBi omb = new OneManyBi(args.length == 0 ? "./" : args[0]);
84             logger.log(BasicLevel.INFO, "Example OneManyBi");
85             omb.createMIofA();
86             omb.createMIofB();
87             omb.generate();
88         } catch (Exception JavaDoc e) {
89             logger.log(BasicLevel.ERROR, "", getInner(e));
90         }
91     }
92     public OneManyBi(String JavaDoc output) throws PException {
93         super(PROJECT_NAME, output);
94     }
95
96     public void createMIofA() throws PException {
97         logger.log(BasicLevel.INFO, "Create the meta information of the A class");
98         Class JavaDoc aclass = compiler.getMIManager().createClass(A_CLASS_NAME);
99
100         PrimitiveElement ida =
101                 aclass.createPrimitiveElement("ida", PTypeSpace.LONG);
102         PrimitiveElement f1 =
103                         aclass.createPrimitiveElement("f1", PTypeSpace.STRING);
104
105         // class identifier
106
NameDef aNameDef = aclass.createNameDef();
107         aNameDef.setFieldName(ida.getName());
108
109         // mulitvalued reference to B
110
GenClassRef bgcr = aclass.createGenClassRef("myB", "Set");
111         //How the gen class is referenced
112
NameDef myBNameDef = bgcr.createRefNameDef();
113         myBNameDef.setFieldName(ida.getName());
114
115         //how the gen clas is identified
116
ScalarField fka = bgcr.createHiddenField("fka", PTypeSpace.LONG);
117         NameDef gcrIdNd = bgcr.createIdNameDef();
118         gcrIdNd.setFieldName("fka");
119
120         //element of the gen class
121
Class JavaDoc bclass = compiler.getMIManager().createClass(B_CLASS_NAME);
122         ClassRef elemCr = bgcr.createClassRef(bclass);
123         ScalarField idb = bgcr.createHiddenField("idb", PTypeSpace.LONG);
124         NameDef gcrElemNd = elemCr.createRefNameDef();
125         gcrElemNd.setFieldName("idb");
126
127
128         // ----------------- Mapping for A -----------------
129
ClassProject acp = aclass.createClassProject(PROJECT_NAME);
130         RdbMapping am = (RdbMapping) acp.createMapping("rdb");
131
132         RdbClassMapping acm = am.createClassMapping("to-table");
133         RdbTable tableA = acm.createRdbTable("table_A");
134         //Identifier of the A class
135
acm.createIdentifierMapping(aNameDef);
136
137         // Mapping rule of the reference myB
138
acm.createReferenceMapping("embedded-target-reference", myBNameDef);
139
140         // Mapping of the ida field over the pka_col
141
tableA.createPrimitiveElementMapping(ida, "pka_col", null, true);
142         // Mapping of the f1 field over the f1_col
143
tableA.createPrimitiveElementMapping(f1, "f1_col", null, true);
144
145
146         //Mapping of the reference to B (multivalued)
147
RdbGenClassMapping gccm =
148                 am.createGenClassMapping("embedded-target-objects", bgcr);
149         //The table of the generic class is also the table of B
150
RdbTable tableGC = gccm.createRdbTable("table_B");
151         tableGC.setReadOnly(true); //optimisaton to avoid writings
152
tableGC.createPrimitiveElementMapping(idb, "pkb_col", null, false);
153         tableGC.createPrimitiveElementMapping(fka, "fka_col", null, false);
154         gccm.createIdentifierMapping(gcrIdNd);
155         gccm.createReferenceMapping("embedded-target-object", gcrElemNd);
156     }
157
158     public void createMIofB() throws PException {
159         logger.log(BasicLevel.INFO, "Create the meta information of the B class");
160         Class JavaDoc bclass = compiler.getMIManager().createClass(B_CLASS_NAME);
161         PrimitiveElement idb =
162                 bclass.createPrimitiveElement("idb", PTypeSpace.LONG);
163         PrimitiveElement f2 =
164                 bclass.createPrimitiveElement("f2", PTypeSpace.STRING);
165
166         // pk simple
167
NameDef bNameDef = bclass.createNameDef();
168         bNameDef.setFieldName("idb");
169
170         // reference sur A
171
ScalarField ida = bclass.createHiddenField("ida", PTypeSpace.LONG);
172         Class JavaDoc aclass = compiler.getMIManager().createClass(A_CLASS_NAME);
173         ClassRef acr = bclass.createClassRef("myA", aclass);
174         NameDef myANameDef = acr.createRefNameDef();
175         myANameDef.setFieldName("ida");
176
177         // ----------------- Mapping for B -----------------
178

179         ClassProject bcp = bclass.createClassProject(PROJECT_NAME);
180         RdbMapping bm = (RdbMapping) bcp.createMapping("rdb");
181
182         RdbClassMultiMapping bcm = bm.createClassMultiMapping("to-table");
183         RdbTable tableB = bcm.createRdbTable("table_B");
184         bcm.createIdentifierMapping(bNameDef);
185
186         bcm.createReferenceMapping("embedded-target-reference", myANameDef);
187
188         tableB.createPrimitiveElementMapping(idb, "pkb_col", null, true);
189         tableB.createPrimitiveElementMapping(f2, "f2_col", null, true);
190         //mapping of the reference to A
191
tableB.createPrimitiveElementMapping(ida, "fka_col");
192     }
193 }
194
Popular Tags