[solved] Create table via API (was Indices)

From: Borut Bolčin (cayenn..mail.si)
Date: Tue Jan 11 2005 - 05:14:28 EST

  • Next message: Borut Bolčin: "Insert ignore"

    Hello,

    I found some time to play with Cayenne's API. I was interested in how to
    programmaticaly create database table out of Cayenne's mappings created
    in modeler.
    Thanks to Mike and Andrus for clues.

    Code will explain better than words:

    try {
        DbAdapter mySqlDbAdapter = new MySQLAdapter();
        MapLoader mapLoader = new MapLoader();
        InputStream is =
    CreateTable.class.getResourceAsStream("MyDataMap.map.xml");
        DataMap dataMap = mapLoader.loadDataMap(new InputSource(is));

        DbGenerator mySqlDbGenerator = new DbGenerator(mySqlDbAdapter, dataMap);
        mySqlDbGenerator.setShouldCreatePKSupport(false); // or true

        // read this info from properties rather than hard code it
        DataSourceInfo dataSourceInfo = new DataSourceInfo();
        
    dataSourceInfo.setAdapterClassName("org.objectstyle.cayenne.dba.JdbcAdapter");
        dataSourceInfo.setDataSourceUrl("jdbc:mysql://localhost/myDatabase");
        dataSourceInfo.setJdbcDriver("com.mysql.jdbc.Driver");
        dataSourceInfo.setPassword("password");
        dataSourceInfo.setUserName("username");

        mySqlDbGenerator.runGenerator(dataSourceInfo);
            
        Artist artist = (Artist) ctxt.createAndRegisterNewObject("Artist");
        artist.setName("Salvador Dali");
        artist.setSomeOtherAttribute("xxxxxxx");
            
        news.setOid(new Integer(100)); // if PK is not provided by database
            
        ctxt.commitChanges(Level.WARN);
            
    } catch (DataMapException e) {
        e.printStackTrace();
    } catch (Exception e) {
        // generator
        e.printStackTrace();
    }

    -Borut



    This archive was generated by hypermail 2.0.0 : Tue Jan 11 2005 - 05:14:36 EST