Re: Configuring custom key generator

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

  • Next message: Gary Jarrel: "Last Record"

    How about in your application constructor after checking for certain
    artifacts in database?

    2006/10/27, Roger Parkinson <roger@senanque.co.nz>:
    >
    > I still need to know where to call something like this at run time so it
    > kicks in for all the tables I use.
    > I can get an adapter and tell it what PkGenerator to use on a case by case
    > basis (as the example in my question shows)
    > But how do I put it in there just once. I don't want to modify everywhere
    > I get a DataContext.
    > Is there some init hook?
    >
    > Roger
    >
    > Borut Bolčina wrote:
    >
    > 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> <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
    >
    >
    >
    >
    > --
    > ------------------------------
    > Roger Parkinson
    > +64 21 508 792
    >
    >



    This archive was generated by hypermail 2.0.0 : Fri Oct 27 2006 - 05:44:31 EDT