1 25 26 27 package org.objectweb.jonas_ejb.lib; 28 29 import java.util.Iterator ; 30 import org.objectweb.jonas.common.Log; 31 import org.objectweb.jonas_ejb.deployment.api.BeanDesc; 32 import org.objectweb.jonas_ejb.deployment.api.DeploymentDescEjb2; 33 import org.objectweb.jonas_ejb.deployment.api.EjbRelationDesc; 34 import org.objectweb.jonas_ejb.deployment.api.EjbRelationshipRoleDesc; 35 import org.objectweb.jonas_ejb.deployment.api.EntityJdbcCmp2Desc; 36 import org.objectweb.jonas_ejb.deployment.api.FieldJdbcDesc; 37 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 38 import org.objectweb.jorm.api.PException; 39 import org.objectweb.jorm.metainfo.api.Class; 40 import org.objectweb.jorm.metainfo.api.ClassProject; 41 import org.objectweb.jorm.metainfo.api.ClassRef; 42 import org.objectweb.jorm.metainfo.api.CompositeName; 43 import org.objectweb.jorm.metainfo.api.GenClassRef; 44 import org.objectweb.jorm.metainfo.api.Manager; 45 import org.objectweb.jorm.metainfo.api.NameDef; 46 import org.objectweb.jorm.metainfo.api.NameRef; 47 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 48 import org.objectweb.jorm.metainfo.api.ScalarField; 49 import org.objectweb.jorm.metainfo.api.PrimitiveElementMapping; 50 import org.objectweb.jorm.metainfo.lib.JormManager; 51 import org.objectweb.jorm.metainfo.lib.MetaInfoPrinter; 52 import org.objectweb.jorm.type.api.PType; 53 import org.objectweb.jorm.mapper.rdb.metainfo.RdbMappingPrinter; 54 import org.objectweb.jorm.mapper.rdb.metainfo.RdbMappingFactory; 55 import org.objectweb.jorm.mapper.rdb.metainfo.RdbMapping; 56 import org.objectweb.jorm.mapper.rdb.metainfo.RdbClassMultiMapping; 57 import org.objectweb.jorm.mapper.rdb.metainfo.RdbTable; 58 import org.objectweb.jorm.mapper.rdb.metainfo.RdbGenClassMapping; 59 import org.objectweb.jorm.mapper.rdb.metainfo.RdbExternalTable; 60 import org.objectweb.jorm.mapper.rdb.metainfo.RdbJoin; 61 import org.objectweb.util.monolog.api.Logger; 62 import org.objectweb.util.monolog.api.BasicLevel; 63 import org.objectweb.util.monolog.wrapper.printwriter.PrintStreamImpl; 64 65 70 public class RdbMappingBuilder { 71 72 75 private Logger logger = null; 76 77 78 81 private JormManager manager = null; 82 83 88 public static final String MAPPER_NAME = "rdb"; 89 90 93 public static final String PROJECT_NAME = "jonas"; 94 95 98 private PrintStreamImpl pstream; 99 private MetaInfoPrinter mip; 100 101 107 public RdbMappingBuilder(DeploymentDescEjb2 dd, Logger logger) throws DeploymentDescException { 108 this.logger = Log.getLogger("org.objectweb.jonas_ejb.mijorm"); 109 pstream = new PrintStreamImpl(this.logger); 110 mip = new MetaInfoPrinter(); 111 mip.addMappingPrinter(new RdbMappingPrinter()); 112 113 manager = new JormManager(); 116 manager.setLogger(logger); 117 manager.init(); 118 manager.addMappingFactory("rdb", new RdbMappingFactory()); 119 120 try { 121 for (Iterator i = dd.getBeanDescIterator(); i.hasNext(); ) { 123 BeanDesc bd = (BeanDesc) i.next(); 124 if (bd instanceof EntityJdbcCmp2Desc) { 125 createJormEntityMI((EntityJdbcCmp2Desc) bd); 126 } 127 } 128 129 for (Iterator i = dd.getEjbRelationDescIterator(); i.hasNext(); ) { 131 EjbRelationDesc relation = (EjbRelationDesc) i.next(); 132 createJormRelationMI(relation); 133 } 134 } catch (PException e) { 135 logger.log(BasicLevel.ERROR, "Jorm Exception raised: " + e); 136 throw new DeploymentDescException(e); 137 } 138 } 139 140 143 public Manager getJormMIManager() { 144 return manager; 145 } 146 147 150 public static String getProjectName() { 151 return PROJECT_NAME; 152 } 153 154 159 private void createJormEntityMI(EntityJdbcCmp2Desc bean) throws PException { 160 161 String asn = bean.getJormClassName(); 162 if (logger.isLoggable(BasicLevel.DEBUG)) { 163 logger.log(BasicLevel.DEBUG, "Create Jorm MI for the bean: " + asn); 164 } 165 166 Class myejb = manager.createClass(asn); 170 bean.addToJormList(myejb); 171 ClassProject acp = myejb.createClassProject(PROJECT_NAME); 172 RdbMapping mapping = (RdbMapping) acp.createMapping(MAPPER_NAME); 173 RdbClassMultiMapping classmapping = mapping.createClassMultiMapping("to-table"); 174 String tablename = bean.getJdbcTableName(); 175 RdbTable mytable = classmapping.createRdbTable(tablename); 176 177 for (Iterator i = bean.getCmpFieldDescIterator(); i.hasNext();) { 181 FieldJdbcDesc fd = (FieldJdbcDesc) i.next(); 182 String fieldname = fd.getName(); 183 if (logger.isLoggable(BasicLevel.DEBUG)) { 184 logger.log(BasicLevel.DEBUG, "Create PrimitiveElement for the CMP field: " + fieldname); 185 } 186 PType pt = JormType.getPType(fd.getFieldType(), fd.isPrimaryKey()); 187 PrimitiveElement pe = myejb.createPrimitiveElement(fieldname, pt, PType.NOSIZE, PType.NOSIZE); 188 mytable.createPrimitiveElementMapping(pe, fd.getJdbcFieldName(), fd.getSqlType(), fd.isPrimaryKey()); 189 } 190 191 NameDef nds = myejb.createNameDef(); 195 if (bean.hasSimplePkField()) { 196 nds.setFieldName(bean.getSimplePkField().getName()); 198 } else { 199 boolean newcn = false; 203 CompositeName cn = manager.getCompositeName(bean.getJormPKClassName()); 204 if (cn == null) { 205 cn = manager.createCompositeName(bean.getJormPKClassName()); 206 bean.addToJormList(cn); 207 newcn = true; 208 } 209 NameRef nrs = nds.createNameRef(cn); 211 for (Iterator it = bean.getCmpFieldDescIterator(); it.hasNext();) { 213 FieldJdbcDesc fd = (FieldJdbcDesc) it.next(); 214 if (fd.isPrimaryKey()) { 215 String fieldName = fd.getName(); 216 if (newcn) { 217 cn.createCompositeNameField(fieldName, JormType.getPType(fd.getFieldType(), fd.isPrimaryKey()), 218 PType.NOSIZE, PType.NOSIZE); 219 } 220 nrs.addProjection(fieldName, fieldName); 221 } 222 } 223 } 224 classmapping.createIdentifierMapping(nds); 225 226 mip.print("Jorm MI for " + asn + ": ", myejb, pstream); 227 mip.print("JORM MI Mapping for " + asn + ": ", classmapping, pstream); 228 } 229 230 235 private void createJormRelationMI(EjbRelationDesc relation) throws PException { 236 if (logger.isLoggable(BasicLevel.DEBUG)) { 237 logger.log(BasicLevel.DEBUG, "Create Jorm MI for the relation: " + relation.getName()); 238 } 239 EjbRelationshipRoleDesc role1 = relation.getRelationshipRole1(); 240 EjbRelationshipRoleDesc role2 = relation.getRelationshipRole2(); 241 if (role1.isTargetMultiple()) 243 createJormRoleMIMulti(role1, role2); 244 else 245 createJormRoleMIOne(role1, role2); 246 if (role2.isTargetMultiple()) 248 createJormRoleMIMulti(role2, role1); 249 else 250 createJormRoleMIOne(role2, role1); 251 } 252 253 260 private void createJormRoleMIOne(EjbRelationshipRoleDesc sRole, EjbRelationshipRoleDesc tRole) 261 throws PException { 262 263 if (logger.isLoggable(BasicLevel.DEBUG)) { 264 logger.log(BasicLevel.DEBUG, sRole.getName()); 265 } 266 267 EntityJdbcCmp2Desc sEntity = (EntityJdbcCmp2Desc) sRole.getSourceBean(); 269 String sAsn = sEntity.getJormClassName(); 270 Class sClass = manager.getClass(sAsn); 271 272 EntityJdbcCmp2Desc tEntity = (EntityJdbcCmp2Desc) sRole.getTargetBean(); 274 String tAsn = tEntity.getJormClassName(); 275 Class tClass = manager.getClass(tAsn); 276 277 ClassProject cpro = sClass.getClassProject(PROJECT_NAME); 278 RdbMapping rdbmapping = (RdbMapping) cpro.getMapping(MAPPER_NAME); 279 RdbClassMultiMapping classmapping = (RdbClassMultiMapping) rdbmapping.getClassMapping(); 280 RdbTable rdbtable = classmapping.getRdbTable(); 281 282 String sCmr = sRole.getCmrFieldName(); 285 286 if (tClass == sClass && sClass.getTypedElement(sCmr) != null) { 288 if (logger.isLoggable(BasicLevel.DEBUG)) { 289 logger.log(BasicLevel.DEBUG, "Nothing to do, same CMR field."); 290 } 291 return; 292 } 293 294 if (tClass != sClass) { 297 classmapping.addDependency(tClass.getFQName()); 298 } 299 300 ClassRef cr = sClass.createClassRef(sCmr, tClass); 302 NameDef rndf = cr.createRefNameDef(); 303 304 if (sRole.hasJdbcMapping() || tClass == sClass) { 305 classmapping.createReferenceMapping("embedded-target-reference", rndf); 308 if (tClass != sClass) { 309 rdbtable.setColocated(true); 310 rdbtable.setColocatedMaster(true); 311 } 312 if (tEntity.hasSimplePkField()) { 313 FieldJdbcDesc fd = (FieldJdbcDesc) tEntity.getSimplePkField(); 315 String cmrmappingname = sRole.getForeignKeyJdbcName(fd.getJdbcFieldName()); 316 PrimitiveElement pe = null; 317 String clafn = sCmr + "_" + fd.getName(); 318 boolean peHasMapping = false; 319 if (cmrmappingname != null) { 320 pe = getPrimitiveElementByColumn(classmapping, cmrmappingname); 322 } 323 if (pe == null) { 324 pe = sClass.createHiddenField(clafn, 326 JormType.getPType(fd.getFieldType(), fd.isPrimaryKey()), 327 PType.NOSIZE, PType.NOSIZE); 328 } else { 329 peHasMapping = true; 330 } 331 rndf.setFieldName(pe.getName()); 333 if (cmrmappingname != null) { if (!peHasMapping) { 335 rdbtable.createPrimitiveElementMapping(pe, cmrmappingname, fd.getSqlType(), false); 336 } 337 } else if (tClass == sClass) { 338 cmrmappingname = tRole.getForeignKeyJdbcName(fd.getJdbcFieldName()); 340 PrimitiveElementMapping pem = classmapping.getPrimitiveElementMapping(tRole.getCmrFieldName() + "_" + fd.getName()); 341 classmapping.addPrimitiveElementMapping(clafn, pem); 342 } 343 if (logger.isLoggable(BasicLevel.DEBUG)) { 344 logger.log(BasicLevel.DEBUG, "CMR " + sCmr + " is mapped over the field: " + clafn + " / columname: " + cmrmappingname); 345 } 346 } else { 347 NameRef cpk = rndf.createNameRef(tClass.getNameDef("").getNameRef().getCompositeName()); 349 for (Iterator i = tEntity.getCmpFieldDescIterator(); i.hasNext(); ) { 350 FieldJdbcDesc fd = (FieldJdbcDesc) i.next(); 351 if (fd.isPrimaryKey()) { 352 String cmrmappingname = sRole.getForeignKeyJdbcName(fd.getJdbcFieldName()); 353 PrimitiveElement pe = null; 354 String clafn = sCmr + "_" + fd.getName(); 355 boolean peHasMapping = false; 356 if (cmrmappingname != null) { 357 pe = getPrimitiveElementByColumn(classmapping, cmrmappingname); 359 } 360 if (pe == null) { 361 pe = sClass.createHiddenField(clafn, 363 JormType.getPType(fd.getFieldType(), fd.isPrimaryKey()), 364 PType.NOSIZE, PType.NOSIZE); 365 } else { 366 peHasMapping = true; 367 } 368 cpk.addProjection(fd.getName(), pe.getName()); 369 if (cmrmappingname != null) { 370 if (!peHasMapping) { 371 rdbtable.createPrimitiveElementMapping(pe, cmrmappingname, fd.getSqlType(), false); 372 } 373 } else if (tClass == sClass) { 374 cmrmappingname = tRole.getForeignKeyJdbcName(fd.getJdbcFieldName()); 376 PrimitiveElementMapping pem = classmapping.getPrimitiveElementMapping(tRole.getCmrFieldName() + "_" + fd.getName()); 377 classmapping.addPrimitiveElementMapping(clafn, pem); 378 } 379 if (logger.isLoggable(BasicLevel.DEBUG)) { 380 logger.log(BasicLevel.DEBUG, "CMR " + sCmr + " is mapped over the field: " + clafn + " / columname: " + cmrmappingname); 381 } 382 } 383 } 384 } 385 } else { 386 classmapping.createReferenceMapping("multi-table", rndf); 389 RdbExternalTable table2 = classmapping.createRdbExternalTable(tEntity.getJdbcTableName()); 390 table2.setColocated(true); table2.setColocatedMaster(false); 392 table2.setReadOnly(true); 393 RdbJoin joinTo2 = table2.createRdbJoin(sCmr); 394 if (logger.isLoggable(BasicLevel.DEBUG)) { 395 logger.log(BasicLevel.DEBUG, "CMR " + sCmr + " is mapped over the external table: " + tEntity.getJdbcTableName()); 396 } 397 if (sEntity.hasSimplePkField()) { 399 FieldJdbcDesc spk = (FieldJdbcDesc) sEntity.getSimplePkField(); 400 String cmrmappingname = tRole.getForeignKeyJdbcName(spk.getJdbcFieldName()); 401 joinTo2.addJoinColumnNames(spk.getJdbcFieldName(), cmrmappingname); 402 if (logger.isLoggable(BasicLevel.DEBUG)) { 403 logger.log(BasicLevel.DEBUG, "CMR " + sCmr + ": join: " + spk.getJdbcFieldName() + " = " + cmrmappingname); 404 } 405 } else { 406 for (Iterator i = sEntity.getCmpFieldDescIterator(); i.hasNext(); ) { 407 FieldJdbcDesc fd = (FieldJdbcDesc) i.next(); 408 if (fd.isPrimaryKey()) { 409 String cmrmappingname = tRole.getForeignKeyJdbcName(fd.getJdbcFieldName()); 410 joinTo2.addJoinColumnNames(fd.getJdbcFieldName(), cmrmappingname); 411 if (logger.isLoggable(BasicLevel.DEBUG)) { 412 logger.log(BasicLevel.DEBUG, "CMR " + sCmr + ": join: " + fd.getJdbcFieldName() + " = " + cmrmappingname); 413 } 414 } 415 } 416 } 417 if (tEntity.hasSimplePkField()) { 419 FieldJdbcDesc tpk = (FieldJdbcDesc) tEntity.getSimplePkField(); 420 String clafn = sCmr + "_" + tpk.getName(); 421 rndf.setFieldName(clafn); 422 ScalarField sf = sClass.createHiddenField(clafn, 423 JormType.getPType(tpk.getFieldType(), tpk.isPrimaryKey()), 424 PType.NOSIZE, PType.NOSIZE); 425 table2.createPrimitiveElementMapping(sf, tpk.getJdbcFieldName(), tpk.getSqlType(), false, joinTo2); 426 if (logger.isLoggable(BasicLevel.DEBUG)) { 427 logger.log(BasicLevel.DEBUG, "CMR " + sCmr + ": mapped over the field : " + clafn + " column: " + tpk.getJdbcFieldName()); 428 } 429 } else { 430 NameRef cpk = rndf.createNameRef(tClass.getNameDef("").getNameRef().getCompositeName()); 431 for (Iterator i = tEntity.getCmpFieldDescIterator(); i.hasNext(); ) { 432 FieldJdbcDesc fd = (FieldJdbcDesc) i.next(); 433 if (fd.isPrimaryKey()) { 434 String clafn = sCmr + "_" + fd.getName(); 435 ScalarField sf = sClass.createHiddenField(clafn, 436 JormType.getPType(fd.getFieldType(), fd.isPrimaryKey()), 437 PType.NOSIZE, PType.NOSIZE); 438 table2.createPrimitiveElementMapping(sf, fd.getJdbcFieldName(), fd.getSqlType(), false, joinTo2); 439 cpk.addProjection(fd.getName(), sf.getName()); 440 if (logger.isLoggable(BasicLevel.DEBUG)) { 441 logger.log(BasicLevel.DEBUG, "CMR " + sCmr + ": mapped over the field : " + clafn + " column: " + fd.getJdbcFieldName()); 442 } 443 } 444 } 445 } 446 } 447 mip.print("JORM MI for " + sAsn + ": ", sClass, pstream); 448 mip.print("JORM MI Mapping for " + sAsn + ": ", classmapping, pstream); 449 } 450 451 457 private void createJormRoleMIMulti(EjbRelationshipRoleDesc sRole, EjbRelationshipRoleDesc tRole) 458 throws PException { 459 460 if (logger.isLoggable(BasicLevel.DEBUG)) { 461 logger.log(BasicLevel.DEBUG, sRole.getName()); 462 } 463 464 EntityJdbcCmp2Desc sEntity = (EntityJdbcCmp2Desc) sRole.getSourceBean(); 466 String sAsn = sEntity.getJormClassName(); 467 Class sClass = manager.getClass(sAsn); 468 469 EntityJdbcCmp2Desc tEntity = (EntityJdbcCmp2Desc) sRole.getTargetBean(); 471 String tAsn = tEntity.getJormClassName(); 472 Class tClass = manager.getClass(tAsn); 473 474 ClassProject cpro = sClass.getClassProject(PROJECT_NAME); 475 RdbMapping rdbmapping = (RdbMapping) cpro.getMapping(MAPPER_NAME); 476 RdbClassMultiMapping classmapping = (RdbClassMultiMapping) rdbmapping.getClassMapping(); 477 478 String sCmr = sRole.getCmrFieldName(); 481 482 if (tClass == sClass && sClass.getTypedElement(sCmr) != null) { 484 if (logger.isLoggable(BasicLevel.DEBUG)) { 485 logger.log(BasicLevel.DEBUG, "Nothing to do, same CMR field."); 486 } 487 return; 488 } 489 490 if (tClass != sClass) { 493 classmapping.addDependency(tClass.getFQName()); 494 } 495 496 GenClassRef gcr = sClass.createGenClassRef(sCmr, sRole.getCmrFieldType().getName()); 498 NameDef ndsg = gcr.createRefNameDef(); 499 500 RdbGenClassMapping gcm = null; 501 RdbTable tableGC = null; 502 if (sRole.isSourceMultiple()) { 503 if (logger.isLoggable(BasicLevel.DEBUG)) { 504 logger.log(BasicLevel.DEBUG, "Create GenClassReference for the CMR field (Many-Many): " + sCmr); 505 } 506 gcm = rdbmapping.createGenClassMapping("embedded-target-references", gcr); 508 tableGC = gcm.createRdbTable(sRole.getRelation().getJdbcTableName()); 510 if (sRole.isSlave()) { 511 tableGC.setReadOnly(true); 512 } 513 } else { 514 if (logger.isLoggable(BasicLevel.DEBUG)) { 515 logger.log(BasicLevel.DEBUG, "Create GenClassReference for the CMR field (One-Many): " + sCmr); 516 } 517 gcm = rdbmapping.createGenClassMapping("embedded-target-objects", gcr); 519 tableGC = gcm.createRdbTable(tEntity.getJdbcTableName()); 520 tableGC.setReadOnly(true); 521 tableGC.setColocated(true); 522 } 523 524 classmapping.createReferenceMapping("embedded-target-reference", ndsg); 528 if (sEntity.hasSimplePkField()) { 529 FieldJdbcDesc fd = (FieldJdbcDesc) sEntity.getSimplePkField(); 531 ndsg.setFieldName(fd.getName()); 532 } else { 533 NameRef cpk = ndsg.createNameRef(sClass.getNameDef("").getNameRef().getCompositeName()); 535 for (Iterator i = sEntity.getCmpFieldDescIterator(); i.hasNext(); ) { 536 FieldJdbcDesc fd = (FieldJdbcDesc) i.next(); 537 if (fd.isPrimaryKey()) { 538 cpk.addProjection(fd.getName(), fd.getName()); 540 } 541 } 542 } 543 544 String fieldsuffix; 547 if (sClass == tClass) { 548 fieldsuffix = (sRole.isSlave() ? "2" : "1"); 549 } else { 550 fieldsuffix = ""; 551 } 552 NameDef ndid = gcr.createIdNameDef(); 553 if (sEntity.hasSimplePkField()) { 554 FieldJdbcDesc fd = (FieldJdbcDesc) sEntity.getSimplePkField(); 556 String clafn = sAsn + "_" + fd.getName() + fieldsuffix; 557 ndid.setFieldName(clafn); 558 ScalarField pe = gcr.createHiddenField(clafn, 559 JormType.getPType(fd.getFieldType(), fd.isPrimaryKey()), 560 PType.NOSIZE, PType.NOSIZE); 561 562 String fkname = tRole.getForeignKeyJdbcName(fd.getJdbcFieldName()); 563 tableGC.createPrimitiveElementMapping(pe, fkname, fd.getSqlType(), false); 564 if (logger.isLoggable(BasicLevel.DEBUG)) { 565 logger.log(BasicLevel.DEBUG, "GC id is mapped over the field: " + clafn + " / column:" + fkname); 566 } 567 } else { 568 NameRef cpk = ndid.createNameRef(sClass.getNameDef("").getNameRef().getCompositeName()); 570 for (Iterator i = sEntity.getCmpFieldDescIterator(); i.hasNext(); ) { 571 FieldJdbcDesc fd = (FieldJdbcDesc) i.next(); 572 if (fd.isPrimaryKey()) { 573 String clafn = sAsn + "_" + fd.getName() + fieldsuffix; 574 ScalarField sf = gcr.createHiddenField(clafn, 575 JormType.getPType(fd.getFieldType(), fd.isPrimaryKey()), 576 PType.NOSIZE, PType.NOSIZE); 577 cpk.addProjection(fd.getName(), sf.getName()); 578 String fkname = tRole.getForeignKeyJdbcName(fd.getJdbcFieldName()); 579 tableGC.createPrimitiveElementMapping(sf, fkname, fd.getSqlType(), false); 580 if (logger.isLoggable(BasicLevel.DEBUG)) { 581 logger.log(BasicLevel.DEBUG, "GC id is mapped over the field: " + clafn + " / column:" + fkname); 582 } 583 } 584 } 585 } 586 gcm.createIdentifierMapping(ndid); 587 588 if (sClass == tClass) { 592 fieldsuffix = (tRole.isSlave() ? "2" : "1"); 593 } else { 594 fieldsuffix = ""; 595 } 596 NameDef ndr2 = gcr.createClassRef(tClass).createRefNameDef(); 598 gcm.createReferenceMapping("embedded-target-object", ndr2); 599 600 if (tEntity.hasSimplePkField()) { 601 FieldJdbcDesc fd = (FieldJdbcDesc) tEntity.getSimplePkField(); 603 String fkname; 604 if (sRole.isSourceMultiple()) { 605 fkname = sRole.getForeignKeyJdbcName(fd.getJdbcFieldName()); 607 } else { 608 fkname = fd.getJdbcFieldName(); 610 } 611 ScalarField pe = getPrimitiveElementByColumn(gcm, fkname); 612 if (pe == null) { 613 String clafn = tAsn + "_" + fd.getName() + fieldsuffix; 614 pe = gcr.createHiddenField(clafn, 615 JormType.getPType(fd.getFieldType(), fd.isPrimaryKey()), 616 PType.NOSIZE, PType.NOSIZE); 617 tableGC.createPrimitiveElementMapping(pe, fkname, fd.getSqlType(), false); 618 } 619 ndr2.setFieldName(pe.getName()); 620 if (logger.isLoggable(BasicLevel.DEBUG)) { 621 logger.log(BasicLevel.DEBUG, "GC elem is mapped over the field: " + pe.getName() + " / column:" + fkname); 622 } 623 } else { 624 NameRef cpk = ndr2.createNameRef(tClass.getNameDef("").getNameRef().getCompositeName()); 626 for (Iterator i = tEntity.getCmpFieldDescIterator(); i.hasNext(); ) { 627 FieldJdbcDesc fd = (FieldJdbcDesc) i.next(); 628 if (fd.isPrimaryKey()) { 629 String fkname; 630 if (sRole.isSourceMultiple()) { 631 fkname = sRole.getForeignKeyJdbcName(fd.getJdbcFieldName()); 633 } else { 634 fkname = fd.getJdbcFieldName(); 636 } 637 ScalarField pe = getPrimitiveElementByColumn(gcm, fkname); 638 if (pe == null) { 639 String clafn = tAsn + "_" + fd.getName() + fieldsuffix; 640 pe = gcr.createHiddenField(clafn, 641 JormType.getPType(fd.getFieldType(), fd.isPrimaryKey()), 642 PType.NOSIZE, PType.NOSIZE); 643 tableGC.createPrimitiveElementMapping(pe, fkname, fd.getSqlType(), false); 644 } 645 cpk.addProjection(fd.getName(), pe.getName()); 646 if (logger.isLoggable(BasicLevel.DEBUG)) { 647 logger.log(BasicLevel.DEBUG, "GC elem is mapped over the field: " + pe.getName() + " / column:" + fkname); 648 } 649 } 650 } 651 } 652 mip.print("JORM MI for " + sAsn + ": ", sClass, pstream); 653 mip.print("JORM MI Mapping for " + sAsn + ": ", classmapping, pstream); 654 } 655 656 private PrimitiveElement getPrimitiveElementByColumn(RdbClassMultiMapping cm, String col) { 657 PrimitiveElementMapping pem = cm.getRdbTable().getPrimitiveElementMappingByCol(col); 658 if (pem == null) { 659 if (logger.isLoggable(BasicLevel.DEBUG)) { 660 logger.log(BasicLevel.DEBUG, "no " + col); 661 } 662 return null; 663 } 664 if (logger.isLoggable(BasicLevel.DEBUG)) { 665 logger.log(BasicLevel.DEBUG, "yes " + col); 666 } 667 return (PrimitiveElement) pem.getLinkedMO(); 668 } 669 670 private ScalarField getPrimitiveElementByColumn(RdbGenClassMapping cm, String col) { 671 PrimitiveElementMapping pem = cm.getRdbTable().getPrimitiveElementMappingByCol(col); 672 if (pem == null) { 673 if (logger.isLoggable(BasicLevel.DEBUG)) { 674 logger.log(BasicLevel.DEBUG, "no " + col); 675 } 676 return null; 677 } 678 if (logger.isLoggable(BasicLevel.DEBUG)) { 679 logger.log(BasicLevel.DEBUG, "yes " + col); 680 } 681 return (ScalarField) pem.getLinkedMO(); 682 } 683 } 684 | Popular Tags |