cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Borut BolĨina" <borut.bolc...@gmail.com>
Subject Re: Configuring custom key generator
Date Fri, 27 Oct 2006 06:35:36 GMT
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.
    */
   @SuppressWarnings("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 <roger@senanque.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
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message