KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > OneOneBiCPK


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.jorm.api.PException;
25 import org.objectweb.jorm.metainfo.api.Class;
26 import org.objectweb.jorm.metainfo.api.ClassProject;
27 import org.objectweb.jorm.metainfo.api.ClassRef;
28 import org.objectweb.jorm.metainfo.api.CompositeName;
29 import org.objectweb.jorm.metainfo.api.NameDef;
30 import org.objectweb.jorm.metainfo.api.NameRef;
31 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
32 import org.objectweb.jorm.metainfo.api.ScalarField;
33 import org.objectweb.jorm.metainfo.rdb.api.RdbClassMapping;
34 import org.objectweb.jorm.metainfo.rdb.api.RdbClassMultiMapping;
35 import org.objectweb.jorm.metainfo.rdb.api.RdbExternalTable;
36 import org.objectweb.jorm.metainfo.rdb.api.RdbJoin;
37 import org.objectweb.jorm.metainfo.rdb.api.RdbMapping;
38 import org.objectweb.jorm.metainfo.rdb.api.RdbTable;
39 import org.objectweb.jorm.type.api.PTypeSpace;
40 import org.objectweb.util.monolog.api.BasicLevel;
41
42 /**
43  * This example builds the jorm meta information for a simple relation between
44  * two persistent object. The A objects has a reference 'myB' to a B object.
45  * whereas the B has a reference 'myA' to the A object. This is a one-one
46  * relationship.
47  * In a mapping point of view the objects are stored on sql table with the
48  * following schema
49  * table_A(pka1_col, pka2_col, f1_col, fkb1_col, fkb2_col)
50  * table_A(pkb1_col, pkb2_col, f2_col)
51  *
52  * S.Chassande-Barrioz
53  */

54 public class OneOneBiCPK extends JormcRunner {
55
56     public final static String JavaDoc PROJECT_NAME = "jormExamples";
57     public final static String JavaDoc A_CLASS_NAME = "org.objectweb.jorm.examples.oobcpk.A";
58     public final static String JavaDoc A_PK_COMPOSITE_NAME = "org.objectweb.jorm.examples.oobcpk.APk";
59     public final static String JavaDoc B_CLASS_NAME = "org.objectweb.jorm.examples.oobcpk.B";
60     public final static String JavaDoc B_PK_COMPOSITE_NAME = "org.objectweb.jorm.examples.oobcpk.BPk";
61
62
63     public static void main(String JavaDoc[] args) {
64         try {
65             OneOneBiCPK oobcpk = new OneOneBiCPK(args.length == 0 ? "./" : args[0]);
66             logger.log(BasicLevel.INFO, "Example OneOneBiCPK");
67             oobcpk.createMIofA();
68             oobcpk.createMIofB();
69             oobcpk.generate();
70         } catch (Exception JavaDoc e) {
71             logger.log(BasicLevel.ERROR, "", getInner(e));
72         }
73     }
74     public OneOneBiCPK(String JavaDoc output) throws PException {
75         super(PROJECT_NAME, output);
76     }
77
78     public void createMIofA() throws PException {
79         logger.log(BasicLevel.INFO, "Create the meta information of the A class");
80         Class JavaDoc aclass = compiler.getMIManager().createClass(A_CLASS_NAME);
81
82         PrimitiveElement ida1 =
83                 aclass.createPrimitiveElement("ida1", PTypeSpace.STRING);
84         PrimitiveElement ida2 =
85                 aclass.createPrimitiveElement("ida2", PTypeSpace.LONG);
86         PrimitiveElement f1 =
87                         aclass.createPrimitiveElement("f1", PTypeSpace.STRING);
88
89         // pk composite
90

91         NameDef aNameDef = aclass.createNameDef();
92         NameRef nr = aNameDef.createNameRef(getACompositeName());
93         nr.addProjection("foo1", ida1.getName());
94         nr.addProjection("foo2", ida2.getName());
95
96         // reference to B
97
ScalarField idb1 = aclass.createHiddenField("idb1", PTypeSpace.BYTE);
98         ScalarField idb2 = aclass.createHiddenField("idb2", PTypeSpace.INT);
99         Class JavaDoc bclass = compiler.getMIManager().createClass(B_CLASS_NAME);
100         ClassRef bcr = aclass.createClassRef("myB", bclass);
101         NameDef myBNameDef = bcr.createRefNameDef();
102         NameRef myBnr = myBNameDef.createNameRef(getBCompositeName());
103         myBnr.addProjection("bar1", idb1.getName());
104         myBnr.addProjection("bar2", idb2.getName());
105
106         // ----------------- Mapping for A -----------------
107
ClassProject acp = aclass.createClassProject(PROJECT_NAME);
108         RdbMapping am = (RdbMapping) acp.createMapping("rdb");
109
110         RdbClassMapping acm = am.createClassMapping("to-table");
111         RdbTable tableA = acm.createRdbTable("table_A");
112         //Identifier of the A class
113
acm.createIdentifierMapping(aNameDef);
114
115         // Mapping rule of the reference myB
116
acm.createReferenceMapping("embedded-target-reference", myBNameDef);
117
118         // Mapping of the ida field over the pka_col
119
tableA.createPrimitiveElementMapping(ida1, "pka1_col", null, true);
120         tableA.createPrimitiveElementMapping(ida2, "pka2_col", null, true);
121         // Mapping of the f1 field over the f1_col
122
tableA.createPrimitiveElementMapping(f1, "f1_col", null, true);
123         // Mapping of the idb field over the fkb_col
124
tableA.createPrimitiveElementMapping(idb1, "fkb1_col", null, false);
125         tableA.createPrimitiveElementMapping(idb2, "fkb2_col", null, false);
126     }
127
128     public void createMIofB() throws PException {
129         logger.log(BasicLevel.INFO, "Create the meta information of the B class");
130         Class JavaDoc bclass = compiler.getMIManager().createClass(B_CLASS_NAME);
131         PrimitiveElement idb1 =
132                 bclass.createPrimitiveElement("idb1", PTypeSpace.BYTE);
133         PrimitiveElement idb2 =
134                 bclass.createPrimitiveElement("idb2", PTypeSpace.INT);
135         PrimitiveElement f2 =
136                 bclass.createPrimitiveElement("f2", PTypeSpace.STRING);
137
138         // pk simple
139
NameDef bNameDef = bclass.createNameDef();
140         NameRef nr = bNameDef.createNameRef(getBCompositeName());
141         nr.addProjection("bar1", idb1.getName());
142         nr.addProjection("bar2", idb2.getName());
143
144         // reference to A
145
ScalarField ida1 = bclass.createHiddenField("ida1", PTypeSpace.STRING);
146         ScalarField ida2 = bclass.createHiddenField("ida2", PTypeSpace.LONG);
147         Class JavaDoc aclass = compiler.getMIManager().createClass(A_CLASS_NAME);
148         ClassRef acr = bclass.createClassRef("myA", aclass);
149         NameDef myANameDef = acr.createRefNameDef();
150         NameRef myAnr = myANameDef.createNameRef(getACompositeName());
151         myAnr.addProjection("foo1", ida1.getName());
152         myAnr.addProjection("foo2", ida2.getName());
153
154
155         // ----------------- Mapping for B -----------------
156

157         ClassProject bcp = bclass.createClassProject(PROJECT_NAME);
158         RdbMapping bm = (RdbMapping) bcp.createMapping("rdb");
159
160         RdbClassMultiMapping bcm = bm.createClassMultiMapping("to-table");
161         RdbTable tableB = bcm.createRdbTable("table_B");
162         bcm.createIdentifierMapping(bNameDef);
163
164         bcm.createReferenceMapping("multi-table", myANameDef);
165
166         tableB.createPrimitiveElementMapping(idb1, "pkb1_col", null, true);
167         tableB.createPrimitiveElementMapping(idb2, "pkb2_col", null, true);
168         tableB.createPrimitiveElementMapping(f2, "f2_col", null, true);
169
170         //Mapping of the reference to A
171
RdbExternalTable extTableA = bcm.createRdbExternalTable("table_A");
172         extTableA.setReadOnly(true); // optimisation to avoid writings
173
extTableA.createPrimitiveElementMapping(ida1, "pka1_col");
174         extTableA.createPrimitiveElementMapping(ida2, "pka2_col");
175         RdbJoin joinToA = extTableA.createRdbJoin("myA_Join");
176         joinToA.addJoinColumnNames("pkb1_col", "fkb1_col");
177         joinToA.addJoinColumnNames("pkb2_col", "fkb2_col");
178     }
179
180     public CompositeName getACompositeName() {
181         CompositeName acn = compiler.getMIManager()
182                 .getCompositeName(A_PK_COMPOSITE_NAME);
183         if (acn == null) {
184             acn = compiler.getMIManager()
185                     .createCompositeName(A_PK_COMPOSITE_NAME);
186             acn.createCompositeNameField("foo1", PTypeSpace.STRING);
187             acn.createCompositeNameField("foo2", PTypeSpace.LONG);
188         }
189         return acn;
190     }
191     public CompositeName getBCompositeName() {
192         CompositeName bcn = compiler.getMIManager()
193                 .getCompositeName(B_PK_COMPOSITE_NAME);
194         if (bcn == null) {
195             bcn = compiler.getMIManager()
196                     .createCompositeName(B_PK_COMPOSITE_NAME);
197             bcn.createCompositeNameField("bar1", PTypeSpace.BYTE);
198             bcn.createCompositeNameField("bar2", PTypeSpace.INT);
199         }
200         return bcn;
201     }
202
203 }
204
Popular Tags