cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Roger Parkinson <ro...@senanque.co.nz>
Subject Re: Configuring custom key generator
Date Fri, 27 Oct 2006 08:14:52 GMT
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
I still need to know where to call something like this at run time so
it kicks in for all the tables I use.<br>
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)<br>
But how do I put it in there just once. I don't want to modify
everywhere I get a DataContext.<br>
Is there some init hook?<br>
<br>
Roger<br>
<br>
Borut Bolčina wrote:
<blockquote
 cite="midee04ee7f0610262335x4d599a58sd00be638eed3cf7d@mail.gmail.com"
 type="cite">Hello Roger,
  <br>
  <br>
study the method bellow which creates tables on two databases based on
info
  <br>
in datamaps created by modeler.
  <br>
  <br>
hope it helps,
  <br>
Borut
  <br>
  <br>
  /**
  <br>
   * Creates tables. This method is capable of creating tables on
multiple
  <br>
   * database servers, even mixed vendors.
  <br>
   */
  <br>
  @SuppressWarnings("unchecked")
  <br>
  private void createTables() {
  <br>
     MySQLAdapter mysqlDbAdapter = new MySQLAdapter();
  <br>
  <br>
     DbGenerator generator = null;
  <br>
  <br>
     // see <a class="moz-txt-link-freetext" href="http://issues.apache.org/cayenne/browse/CAY-527">http://issues.apache.org/cayenne/browse/CAY-527</a>
  <br>
     //PropertyUtils.setProperty(mysqlDbAdapter, "pkGenerator", new
  <br>
MyJdbcPkGenerator());
  <br>
  <br>
     List&lt;DataMap&gt; maps = (List) dataContext.getEntityResolver
  <br>
().getDataMaps();
  <br>
     for (DataMap dataMap : maps) {
  <br>
        DataNode dataNode = dataContext.getParentDataDomain
  <br>
().lookupDataNode(dataMap);
  <br>
        DataSource dataSource = dataNode.getDataSource();
  <br>
        MyJdbcPkGenerator myPkGenerator = null;
  <br>
        if (dataMap.getName().equals("MyFirstDomainMap")) {
  <br>
           mysqlDbAdapter.setSupportsFkConstraints(false);
  <br>
           generator = new DbGenerator(mysqlDbAdapter, dataMap);
  <br>
  <br>
           generator.setShouldCreatePKSupport(false);
  <br>
           generator.setShouldDropTables(false);
  <br>
        } else if (dataMap.getName().equals("MySecondDomainMap")) {
  <br>
           mysqlDbAdapter.setSupportsFkConstraints(true);
  <br>
           myPkGenerator = new MyJdbcPkGenerator();
  <br>
           myPkGenerator.setDataNode(dataNode);
  <br>
           mysqlDbAdapter.setPkGenerator(myPkGenerator);
  <br>
           generator = new DbGenerator(mysqlDbAdapter, dataMap);
  <br>
  <br>
           generator.setShouldCreatePKSupport(true);
  <br>
           generator.setShouldCreateFKConstraints(true);
  <br>
           generator.setShouldDropTables(false);
  <br>
        }
  <br>
        try {
  <br>
           generator.runGenerator(dataSource);
  <br>
        } catch (Exception e) {
  <br>
           logger.error("Error running database generator for " +
  <br>
dataMap.getName(), e);
  <br>
        }
  <br>
     }
  <br>
  }
  <br>
  <br>
  <br>
2006/10/26, Roger Parkinson <a class="moz-txt-link-rfc2396E" href="mailto:roger@senanque.co.nz">&lt;roger@senanque.co.nz&gt;</a>:
  <br>
  <blockquote type="cite"><br>
 I have extended JdbcPkGenerator to generate a custom primary key and
it
    <br>
works fine in unit testing.
    <br>
How do I tell Cayenne to use my class instead of standard
    <br>
JdbcPkGenerator?
    <br>
    <br>
Searching the messages reveals I should do something like:
    <br>
    <br>
DbAdapter adapter = dataNode.getAdapter();
    <br>
PropertyUtils.setProperty(adapter,"pkGenerator",new
MyJdbcPkGenerator());
    <br>
    <br>
But I don't know where to actually put this code so that it will use my
    <br>
generator everywhere.
    <br>
Thanks
    <br>
--
    <br>
------------------------------
    <br>
Roger Parkinson
    <br>
+64 21 508 792
    <br>
    <br>
    <br>
  </blockquote>
  <br>
</blockquote>
<br>
<div class="moz-signature">-- <br>
<hr size="2" width="100%"><font color="#3366ff" face="Arial"><big>Roger
Parkinson</big><br>
+64 21 508 792<br>
</font><font face="Arial"><br>
</font></div>
</body>
</html>


Mime
View raw message