1 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.GenClassRef; 30 import org.objectweb.jorm.metainfo.api.ScalarField; 31 import org.objectweb.jorm.metainfo.api.ClassRef; 32 import org.objectweb.jorm.metainfo.api.ClassProject; 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.RdbGenClassMapping; 37 import org.objectweb.jorm.metainfo.rdb.api.RdbClassMultiMapping; 38 import org.objectweb.jorm.type.api.PTypeSpace; 39 40 44 public class ManyManyBi extends JormcRunner { 45 46 public final static String PROJECT_NAME = "jormExamples"; 47 public final static String A_CLASS_NAME = "org.objectweb.jorm.examples.mmb.A"; 48 public final static String B_CLASS_NAME = "org.objectweb.jorm.examples.mmb.B"; 49 50 51 public static void main(String [] args) { 52 try { 53 ManyManyBi mmb = new ManyManyBi(args.length == 0 ? "./" : args[0]); 54 logger.log(BasicLevel.INFO, "Example ManyManyBi"); 55 mmb.createMIofA(); 56 mmb.createMIofB(); 57 mmb.generate(); 58 } catch (Exception e) { 59 logger.log(BasicLevel.ERROR, "", getInner(e)); 60 } 61 } 62 public ManyManyBi(String output) throws PException { 63 super(PROJECT_NAME, output); 64 } 65 66 public void createMIofA() throws PException { 67 logger.log(BasicLevel.INFO, "Create the meta information of the A class"); 68 Class aclass = compiler.getMIManager().createClass(A_CLASS_NAME); 69 70 PrimitiveElement ida = 71 aclass.createPrimitiveElement("ida", PTypeSpace.LONG); 72 PrimitiveElement f1 = 73 aclass.createPrimitiveElement("f1", PTypeSpace.STRING); 74 75 NameDef aNameDef = aclass.createNameDef(); 77 aNameDef.setFieldName(ida.getName()); 78 79 GenClassRef bgcr = aclass.createGenClassRef("myB", "Set"); 81 NameDef myBNameDef = bgcr.createRefNameDef(); 83 myBNameDef.setFieldName(ida.getName()); 84 85 ScalarField fka = bgcr.createHiddenField("fka", PTypeSpace.LONG); 87 NameDef gcrIdNd = bgcr.createIdNameDef(); 88 gcrIdNd.setFieldName(fka.getName()); 89 90 Class bclass = compiler.getMIManager().createClass(B_CLASS_NAME); 92 ClassRef elemCr = bgcr.createClassRef(bclass); 93 ScalarField fkb = bgcr.createHiddenField("fkb", PTypeSpace.LONG); 94 NameDef gcrElemNd = elemCr.createRefNameDef(); 95 gcrElemNd.setFieldName(fkb.getName()); 96 97 98 ClassProject acp = aclass.createClassProject(PROJECT_NAME); 100 RdbMapping am = (RdbMapping) acp.createMapping("rdb"); 101 102 RdbClassMapping acm = am.createClassMapping("to-table"); 103 RdbTable tableA = acm.createRdbTable("table_A"); 104 acm.createIdentifierMapping(aNameDef); 106 107 acm.createReferenceMapping("embedded-target-reference", myBNameDef); 109 110 tableA.createPrimitiveElementMapping(ida, "pka_col", null, true); 112 tableA.createPrimitiveElementMapping(f1, "f1_col", null, true); 114 115 116 RdbGenClassMapping gccm = 117 am.createGenClassMapping("embedded-target-references", bgcr); 118 RdbTable tableGC = gccm.createRdbTable("table_JOIN"); 119 120 tableGC.createPrimitiveElementMapping(fka, "fka_col", null, true); 121 tableGC.createPrimitiveElementMapping(fkb, "fkb_col", null, true); 122 gccm.createIdentifierMapping(gcrIdNd); 123 gccm.createReferenceMapping("embedded-target-reference", gcrElemNd); 124 } 125 126 public void createMIofB() throws PException { 127 logger.log(BasicLevel.INFO, "Create the meta information of the B class"); 128 Class bclass = compiler.getMIManager().createClass(B_CLASS_NAME); 129 PrimitiveElement idb = 130 bclass.createPrimitiveElement("idb", PTypeSpace.LONG); 131 PrimitiveElement f2 = 132 bclass.createPrimitiveElement("f2", PTypeSpace.STRING); 133 134 NameDef bNameDef = bclass.createNameDef(); 136 bNameDef.setFieldName(idb.getName()); 137 138 GenClassRef agcr = bclass.createGenClassRef("myA", "Set"); 140 NameDef myANameDef = agcr.createRefNameDef(); 142 myANameDef.setFieldName(idb.getName()); 143 144 ScalarField fkb = agcr.createHiddenField("fkb", PTypeSpace.LONG); 146 NameDef gcrIdNd = agcr.createIdNameDef(); 147 gcrIdNd.setFieldName(fkb.getName()); 148 149 Class aclass = compiler.getMIManager().createClass(A_CLASS_NAME); 151 ClassRef elemCr = agcr.createClassRef(aclass); 152 ScalarField fka = agcr.createHiddenField("fka", PTypeSpace.LONG); 153 NameDef gcrElemNd = elemCr.createRefNameDef(); 154 gcrElemNd.setFieldName("fka"); 155 156 158 ClassProject bcp = bclass.createClassProject(PROJECT_NAME); 159 RdbMapping bm = (RdbMapping) bcp.createMapping("rdb"); 160 161 RdbClassMultiMapping bcm = bm.createClassMultiMapping("to-table"); 162 RdbTable tableB = bcm.createRdbTable("table_B"); 163 bcm.createIdentifierMapping(bNameDef); 164 165 bcm.createReferenceMapping("embedded-target-reference", myANameDef); 166 167 tableB.createPrimitiveElementMapping(idb, "pkb_col", null, true); 168 tableB.createPrimitiveElementMapping(f2, "f2_col", null, true); 169 170 RdbGenClassMapping gccm = 171 bm.createGenClassMapping("embedded-target-references", agcr); 172 RdbTable tableGC = gccm.createRdbTable("table_JOIN"); 173 174 tableGC.createPrimitiveElementMapping(fkb, "fkb_col", null, true); 175 tableGC.createPrimitiveElementMapping(fka, "fka_col", null, true); 176 gccm.createIdentifierMapping(gcrIdNd); 177 gccm.createReferenceMapping("embedded-target-reference", gcrElemNd); 178 } 179 180 } 181 | Popular Tags |