Re: Configuring custom key generator

From: Borut Bolčina (borut.bolcin..mail.com)
Date: Fri Oct 27 2006 - 02:35:36 EDT

  • Next message: Roger Parkinson: "Re: Configuring custom key generator"

    Hello Roger,

    study the method bellow which creates tables on two databases based on info
    in datamaps created by modeler.

    hope it helps,
    Borut

       /**
        * Creates tables. This method is capable of creating tables on multiple
        * database servers, even mixed vendors.
        */
      ..uppressWarnings("unchecked")
       private void createTables() {
          MySQLAdapter mysqlDbAdapter = new MySQLAdapter();

          DbGenerator generator = null;

          // see http://issues.apache.org/cayenne/browse/CAY-527
          //PropertyUtils.setProperty(mysqlDbAdapter, "pkGenerator", new
    MyJdbcPkGenerator());

          List<DataMap> maps = (List) dataContext.getEntityResolver
    ().getDataMaps();
          for (DataMap dataMap : maps) {
             DataNode dataNode = dataContext.getParentDataDomain
    ().lookupDataNode(dataMap);
             DataSource dataSource = dataNode.getDataSource();
             MyJdbcPkGenerator myPkGenerator = null;
             if (dataMap.getName().equals("MyFirstDomainMap")) {
                mysqlDbAdapter.setSupportsFkConstraints(false);
                generator = new DbGenerator(mysqlDbAdapter, dataMap);

                generator.setShouldCreatePKSupport(false);
                generator.setShouldDropTables(false);
             } else if (dataMap.getName().equals("MySecondDomainMap")) {
                mysqlDbAdapter.setSupportsFkConstraints(true);
                myPkGenerator = new MyJdbcPkGenerator();
                myPkGenerator.setDataNode(dataNode);
                mysqlDbAdapter.setPkGenerator(myPkGenerator);
                generator = new DbGenerator(mysqlDbAdapter, dataMap);

                generator.setShouldCreatePKSupport(true);
                generator.setShouldCreateFKConstraints(true);
                generator.setShouldDropTables(false);
             }
             try {
                generator.runGenerator(dataSource);
             } catch (Exception e) {
                logger.error("Error running database generator for " +
    dataMap.getName(), e);
             }
          }
       }

    2006/10/26, Roger Parkinson <roge..enanque.co.nz>:
    >
    > I have extended JdbcPkGenerator to generate a custom primary key and it
    > works fine in unit testing.
    > How do I tell Cayenne to use my class instead of standard
    > JdbcPkGenerator?
    >
    > Searching the messages reveals I should do something like:
    >
    > DbAdapter adapter = dataNode.getAdapter();
    > PropertyUtils.setProperty(adapter,"pkGenerator",new MyJdbcPkGenerator());
    >
    > But I don't know where to actually put this code so that it will use my
    > generator everywhere.
    > Thanks
    > --
    > ------------------------------
    > Roger Parkinson
    > +64 21 508 792
    >
    >



    This archive was generated by hypermail 2.0.0 : Fri Oct 27 2006 - 02:36:04 EDT