You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Alexey Shakov <al...@menta.de> on 2011/11/14 10:28:59 UTC

How to configure Cayenne and map generic object programmatically from scratch without using of XML

Hi *,

I would like to know, how to  configure Cayenne and map generic object 
programmatically from scratch without using of XML?

Most likely, I need to define a DataSource (Factory), create DataDomain, 
some DataMaps, containing my generic Entities, etc.

Are there some examples, how to do this using last Cayenne milestone 
release?

Many thanks in advance for any help!

Greetings,

Alexey




Re: How to configure Cayenne and map generic object programmatically from scratch without using of XML

Posted by Andrus Adamchik <an...@objectstyle.org>.
Here is a bit of 3.1 specifics on top of the suggestion below. In 3.1 you'd configure a custom Module with providers/factories for everything you need. Inside the module you'll need to bind DataSourceFactory and Provider<DataDomain>. Then you create a stack with the custom module:

ServerRuntime runtime = new ServerRuntime("", new MyModule());

And then code your implementations of DataSourceFactory and Provider<DataDomain>. Inside the later, you'd create a DataDomain with attached DataMaps and DataNodes.

Andrus

On Nov 14, 2011, at 12:44 PM, Tercio Gaudencio Filho wrote:

> Hi Alexey,
> 
> I'm trying exactly that.
> 
> final DbAttribute attr = new DbAttribute("id");
> attr.setType(Types.INTEGER);
> attr.setPrimaryKey(true);
> attr.setMandatory(true);
> 
> final DbEntity entity = new DbEntity("TEST");
> entity.addAttribute(attr);
> 
> final DataMap map = new DataMap("AppMap");
> map.addDbEntity(entity);
> 
> final DataDomain domain = new DataDomain("App");
> domain.addMap(map);
> 
> final DataNode node = new DataNode("AppNode");
> node.setDataSourceLocation("dbcp.properties");
> node.setDataSourceFactory(org.apache.cayenne.conf.DBCPDataSourceFactory.class.getName());
> node.setSchemaUpdateStrategy(new org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy());
> node.addDataMap(map);
> node.setDataSource(new org.apache.cayenne.conf.DBCPDataSourceFactory().getDataSource(node.getDataSourceLocation()));
> node.setAdapter(new MySQLAdapter());
> domain.addNode(node);
> 
> final Configuration c = Configuration.getSharedConfiguration();
> c.addDomain(domain);
> 
> final ObjectContext context = DataContext.createDataContext("App");
> 
> In this implementation I need to figure out how to "inject" the DataSource into the Node in a more "clean" way, in this example it is, at least, weird.
> 
> As the line Configuration.getSharedConfiguration() will instantiate a DefaultConfiguration class, this implementation will look for a cayenne.xml, so you need this file "empty":
> <?xml version="1.0" encoding="utf-8"?>
> <domains project-version="3.0.0.1">
> </domains>
> 
> 
> My next step is to implement a DynamicConfiguration to allow a initialization without any configuration file.
> 
> Currently my example is not ideal, but it's a kick-off.
> 
> By the way, I'm using DBCP as connection pool.
> 
> Regards, 
> 
> --
> Att.
> 
> Tercio Gaudencio Filho
> 
> 
> On Monday, November 14, 2011 at 7:28 AM, Alexey Shakov wrote:
> 
>> Hi *,
>> 
>> I would like to know, how to configure Cayenne and map generic object 
>> programmatically from scratch without using of XML?
>> 
>> Most likely, I need to define a DataSource (Factory), create DataDomain, 
>> some DataMaps, containing my generic Entities, etc.
>> 
>> Are there some examples, how to do this using last Cayenne milestone 
>> release?
>> 
>> Many thanks in advance for any help!
>> 
>> Greetings,
>> 
>> Alexey 
> 


Re: How to configure Cayenne and map generic object programmatically from scratch without using of XML

Posted by Tercio Gaudencio Filho <te...@gmail.com>.
Hi Alexey,

I'm trying exactly that.

final DbAttribute attr = new DbAttribute("id");
attr.setType(Types.INTEGER);
attr.setPrimaryKey(true);
attr.setMandatory(true);

final DbEntity entity = new DbEntity("TEST");
entity.addAttribute(attr);

final DataMap map = new DataMap("AppMap");
map.addDbEntity(entity);

final DataDomain domain = new DataDomain("App");
domain.addMap(map);

final DataNode node = new DataNode("AppNode");
node.setDataSourceLocation("dbcp.properties");
node.setDataSourceFactory(org.apache.cayenne.conf.DBCPDataSourceFactory.class.getName());
node.setSchemaUpdateStrategy(new org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy());
node.addDataMap(map);
node.setDataSource(new org.apache.cayenne.conf.DBCPDataSourceFactory().getDataSource(node.getDataSourceLocation()));
node.setAdapter(new MySQLAdapter());
domain.addNode(node);

final Configuration c = Configuration.getSharedConfiguration();
c.addDomain(domain);

final ObjectContext context = DataContext.createDataContext("App");

In this implementation I need to figure out how to "inject" the DataSource into the Node in a more "clean" way, in this example it is, at least, weird.

As the line Configuration.getSharedConfiguration() will instantiate a DefaultConfiguration class, this implementation will look for a cayenne.xml, so you need this file "empty":
<?xml version="1.0" encoding="utf-8"?>
<domains project-version="3.0.0.1">
</domains>


My next step is to implement a DynamicConfiguration to allow a initialization without any configuration file.

Currently my example is not ideal, but it's a kick-off.

By the way, I'm using DBCP as connection pool.

Regards, 

--
Att.

Tercio Gaudencio Filho


On Monday, November 14, 2011 at 7:28 AM, Alexey Shakov wrote:

> Hi *,
> 
> I would like to know, how to configure Cayenne and map generic object 
> programmatically from scratch without using of XML?
> 
> Most likely, I need to define a DataSource (Factory), create DataDomain, 
> some DataMaps, containing my generic Entities, etc.
> 
> Are there some examples, how to do this using last Cayenne milestone 
> release?
> 
> Many thanks in advance for any help!
> 
> Greetings,
> 
> Alexey