You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Hugi Thordarson <hu...@karlmenn.is> on 2015/05/22 12:33:25 UTC

Sharing a database with EOF: Primary key generation

Hi again.

I’m using EOF and Cayenne to talk to the same MySQL database. Works fine apart from one problem: Primary key generation.

Cayenne and EOF use a similar method, i.e. a table for the primary keys, but there’s an implementation difference — EOF stores the *last* primary key value provided while Cayenne stores the *next* value to be used. This means they can’t share a single table (by using a view for the naming differences).

Anyone have any brilliant ideas or workarounds for this? (before I start messing around with triggers or other messy workarounds).

On the subject of PK generation, there’s one thing I really like in EOF; if the PK table is missing a row for a table, EOF will automatically select the max() value of the table's PK column and use it to populate the table. Quite useful. Any objections to this becoming a feature in Cayenne as well? (via pull request)

Cheers,
- hugi

PS: Sorry for spamming the list with my stupid questions these days: hopefully I’ll be able to contribute in a meaningful way soon, rather than just sucking at the teat of your knowledge :).

// Hugi Thordarson
// http://www.loftfar.is/ <http://www.loftfar.is/>
// s. 895-6688

Re: Sharing a database with EOF: Primary key generation

Posted by Mike Kienenberger <mk...@gmail.com>.
On Fri, May 22, 2015 at 9:46 AM, Mike Kienenberger <mk...@gmail.com> wrote:
> Hugi, it would be worthwhile to make this a customizable parameter in
> the existing primary key generator rather than only creating your own
> custom version.  I also haven't looked at that part of the code base
> in a long while so I don't know how feasible this is.

I'm talking about whether to store the next value or the last value.
It wasn't obvious in my first reply.


> On Fri, May 22, 2015 at 7:15 AM, Andrus Adamchik <an...@objectstyle.org> wrote:
>>> Cayenne and EOF use a similar method, i.e. a table for the primary keys, but there's an implementation difference -- EOF stores the *last* primary key value provided while Cayenne stores the *next* value to be used. This means they can't share a single table (by using a view for the naming differences).
>>>
>>> Anyone have any brilliant ideas or workarounds for this? (before I start messing around with triggers or other messy workarounds).
>>
>> Cayenne has PkGenerator interface that can be customized/reimplemented:
>>
>> http://cayenne.apache.org/docs/4.0/api/org/apache/cayenne/dba/PkGenerator.html
>>
>> Unfortunately it is not directly injectable via DI (this is a TODO), so initializing it will require a bit of indirection. E.g. you may subclass of decorate DbAdapterFactory. Here is a decoration example:
>>
>> new ServerRuntimeBuilder()
>>    .addModule(binder -> binder
>>         .decorate(DbAdapterFactory.class)
>>         .after(MyDbAdapterFactory.class)).build();
>>
>>
>> public class MyDbAdapterFactory implements DbAdapterFactory {
>>    private DbAdapterFactory delegate;
>>
>>    public MyDbAdapterFactory(@Inject DbAdapterFactory delegate) {
>>       this.delegate  = delegate;
>>    }
>>
>>    @Override
>>    public DbAdapter createAdapter(DataNodeDescriptor nodeDescriptor, DataSource dataSource) throws Exception {
>>       DbAdapter a = deletegate.createAdapter(nodeDescriptor, dataSource);
>>       return .. // here decorate "a" to return a custom PkGenerator.
>>    }
>> }
>>
>>> On the subject of PK generation, there's one thing I really like in EOF; if the PK table is missing a row for a table, EOF will automatically select the max() value of the table's PK column and use it to populate the table. Quite useful. Any objections to this becoming a feature in Cayenne as well? (via pull request)
>>
>> I am all +1 on this. I'd say PK generation has been neglected for some time, so we should freshen it up, including auto-bootstrap that you mentioned, and DI integration, maybe smarter categorization by generator strategy, etc.
>>
>> One of the reasons I personally haven't payed much attention to this lately, is cause I am using DB autoincrement (something not supported by EOF) and never had to worry about PK lookup tables, sequences, etc. :)
>>
>> Andrus
>>
>>> On May 22, 2015, at 1:33 PM, Hugi Thordarson <hu...@karlmenn.is> wrote:
>>>
>>> Hi again.
>>>
>>> I'm using EOF and Cayenne to talk to the same MySQL database. Works fine apart from one problem: Primary key generation.
>>>

Re: Sharing a database with EOF: Primary key generation

Posted by Mike Kienenberger <mk...@gmail.com>.
Hugi, it would be worthwhile to make this a customizable parameter in
the existing primary key generator rather than only creating your own
custom version.  I also haven't looked at that part of the code base
in a long while so I don't know how feasible this is.

On Fri, May 22, 2015 at 7:15 AM, Andrus Adamchik <an...@objectstyle.org> wrote:
>> Cayenne and EOF use a similar method, i.e. a table for the primary keys, but there's an implementation difference -- EOF stores the *last* primary key value provided while Cayenne stores the *next* value to be used. This means they can't share a single table (by using a view for the naming differences).
>>
>> Anyone have any brilliant ideas or workarounds for this? (before I start messing around with triggers or other messy workarounds).
>
> Cayenne has PkGenerator interface that can be customized/reimplemented:
>
> http://cayenne.apache.org/docs/4.0/api/org/apache/cayenne/dba/PkGenerator.html
>
> Unfortunately it is not directly injectable via DI (this is a TODO), so initializing it will require a bit of indirection. E.g. you may subclass of decorate DbAdapterFactory. Here is a decoration example:
>
> new ServerRuntimeBuilder()
>    .addModule(binder -> binder
>         .decorate(DbAdapterFactory.class)
>         .after(MyDbAdapterFactory.class)).build();
>
>
> public class MyDbAdapterFactory implements DbAdapterFactory {
>    private DbAdapterFactory delegate;
>
>    public MyDbAdapterFactory(@Inject DbAdapterFactory delegate) {
>       this.delegate  = delegate;
>    }
>
>    @Override
>    public DbAdapter createAdapter(DataNodeDescriptor nodeDescriptor, DataSource dataSource) throws Exception {
>       DbAdapter a = deletegate.createAdapter(nodeDescriptor, dataSource);
>       return .. // here decorate "a" to return a custom PkGenerator.
>    }
> }
>
>> On the subject of PK generation, there's one thing I really like in EOF; if the PK table is missing a row for a table, EOF will automatically select the max() value of the table's PK column and use it to populate the table. Quite useful. Any objections to this becoming a feature in Cayenne as well? (via pull request)
>
> I am all +1 on this. I'd say PK generation has been neglected for some time, so we should freshen it up, including auto-bootstrap that you mentioned, and DI integration, maybe smarter categorization by generator strategy, etc.
>
> One of the reasons I personally haven't payed much attention to this lately, is cause I am using DB autoincrement (something not supported by EOF) and never had to worry about PK lookup tables, sequences, etc. :)
>
> Andrus
>
>> On May 22, 2015, at 1:33 PM, Hugi Thordarson <hu...@karlmenn.is> wrote:
>>
>> Hi again.
>>
>> I'm using EOF and Cayenne to talk to the same MySQL database. Works fine apart from one problem: Primary key generation.
>>
>> Cayenne and EOF use a similar method, i.e. a table for the primary keys, but there's an implementation difference -- EOF stores the *last* primary key value provided while Cayenne stores the *next* value to be used. This means they can't share a single table (by using a view for the naming differences).
>>
>> Anyone have any brilliant ideas or workarounds for this? (before I start messing around with triggers or other messy workarounds).
>>
>> On the subject of PK generation, there's one thing I really like in EOF; if the PK table is missing a row for a table, EOF will automatically select the max() value of the table's PK column and use it to populate the table. Quite useful. Any objections to this becoming a feature in Cayenne as well? (via pull request)
>>
>> Cheers,
>> - hugi
>>
>> PS: Sorry for spamming the list with my stupid questions these days: hopefully I'll be able to contribute in a meaningful way soon, rather than just sucking at the teat of your knowledge :).
>>
>> // Hugi Thordarson
>> // http://www.loftfar.is/ <http://www.loftfar.is/>
>> // s. 895-6688
>

Re: Sharing a database with EOF: Primary key generation

Posted by Andrus Adamchik <an...@objectstyle.org>.
> Cayenne and EOF use a similar method, i.e. a table for the primary keys, but there’s an implementation difference — EOF stores the *last* primary key value provided while Cayenne stores the *next* value to be used. This means they can’t share a single table (by using a view for the naming differences).
> 
> Anyone have any brilliant ideas or workarounds for this? (before I start messing around with triggers or other messy workarounds).

Cayenne has PkGenerator interface that can be customized/reimplemented:

http://cayenne.apache.org/docs/4.0/api/org/apache/cayenne/dba/PkGenerator.html

Unfortunately it is not directly injectable via DI (this is a TODO), so initializing it will require a bit of indirection. E.g. you may subclass of decorate DbAdapterFactory. Here is a decoration example:

new ServerRuntimeBuilder()
   .addModule(binder -> binder
	.decorate(DbAdapterFactory.class)
	.after(MyDbAdapterFactory.class)).build();


public class MyDbAdapterFactory implements DbAdapterFactory {
   private DbAdapterFactory delegate;
  
   public MyDbAdapterFactory(@Inject DbAdapterFactory delegate) {
      this.delegate  = delegate;
   }

   @Override
   public DbAdapter createAdapter(DataNodeDescriptor nodeDescriptor, DataSource dataSource) throws Exception {
      DbAdapter a = deletegate.createAdapter(nodeDescriptor, dataSource);
      return .. // here decorate "a" to return a custom PkGenerator.
   }
}

> On the subject of PK generation, there’s one thing I really like in EOF; if the PK table is missing a row for a table, EOF will automatically select the max() value of the table's PK column and use it to populate the table. Quite useful. Any objections to this becoming a feature in Cayenne as well? (via pull request)

I am all +1 on this. I'd say PK generation has been neglected for some time, so we should freshen it up, including auto-bootstrap that you mentioned, and DI integration, maybe smarter categorization by generator strategy, etc. 

One of the reasons I personally haven't payed much attention to this lately, is cause I am using DB autoincrement (something not supported by EOF) and never had to worry about PK lookup tables, sequences, etc. :)

Andrus

> On May 22, 2015, at 1:33 PM, Hugi Thordarson <hu...@karlmenn.is> wrote:
> 
> Hi again.
> 
> I’m using EOF and Cayenne to talk to the same MySQL database. Works fine apart from one problem: Primary key generation.
> 
> Cayenne and EOF use a similar method, i.e. a table for the primary keys, but there’s an implementation difference — EOF stores the *last* primary key value provided while Cayenne stores the *next* value to be used. This means they can’t share a single table (by using a view for the naming differences).
> 
> Anyone have any brilliant ideas or workarounds for this? (before I start messing around with triggers or other messy workarounds).
> 
> On the subject of PK generation, there’s one thing I really like in EOF; if the PK table is missing a row for a table, EOF will automatically select the max() value of the table's PK column and use it to populate the table. Quite useful. Any objections to this becoming a feature in Cayenne as well? (via pull request)
> 
> Cheers,
> - hugi
> 
> PS: Sorry for spamming the list with my stupid questions these days: hopefully I’ll be able to contribute in a meaningful way soon, rather than just sucking at the teat of your knowledge :).
> 
> // Hugi Thordarson
> // http://www.loftfar.is/ <http://www.loftfar.is/>
> // s. 895-6688