You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by ha...@welinux.cl on 2009/10/05 06:31:43 UTC

Id problem when trying to make a component decoupling a module

Hi,

I've crashed again with the infamous message Can't get primary key from temporary id.

This has happened me constantly in the area of reusing code.

Some weeks ago, i needed to create an objet and then send its id in the url to a web control responsible to filling it. I was forced to commit between the transaction to obtain the id. The process was simple and we were able to live with it.

Now we are forcing something more complex: our company developped a very complex software and in the process we developed a very robust workflow engine to support it.
We decided to decouple the workflow component from the application where it was born. The idea is to make a jar file for using in other projects, and not resort to copy paste.

As you can see, the extraction means decoupling the workflow and associated classes from the rest of the classes of the system.

In order for workflow classes not to depend on any particular class, it was necessary to remove the relationships in cayenne and try to make a soft bind with cayenne using the id of the related workflow in the classes that wich to use it.

The problem is when we try to set the id in a class using the workflow it gives me the aforementioned message of temporary id. I don't want to commit in the middle like i did before in order to get the id, or to try to manually keep the keys of the workflow with a custom sequence or something, or to update the id after commit.

Note: Sharing of the map was very easy and worked fine.

Any help will be appreciated.

Thanks
Hans

Re: Id problem when trying to make a component decoupling a module

Posted by Aristedes Maniatis <ar...@maniatis.org>.
On 5/10/09 3:31 PM, hans@welinux.cl wrote:
> The problem is when we try to set the id in a class using the workflow it gives me the aforementioned message of temporary id. I don't want to commit in the middle like i did before in order to get the id, or to try to manually keep the keys of the workflow with a custom sequence or something, or to update the id after commit.

You'll need to write your library so that it deals with Java objects which have no knowledge of how they are to be persisted in a database. Once you write code which operates on primary keys, you'll either need to persist the objects in a database already or start using UUIDs. Either way is a step away from the promise of an ORM: to abstract the realities of database storage from the objects your code deals with.

As far as your URL problem is concerned, you are pretty much sunk if your application has to be sessionless. Which is why any application which begins to have a sophisticated workflow will need to keep objects server-side within a session. Then you have no requirement to create URLs which embed primary keys.

In short, you are finding it hard to create and manage primary keys in your component mainly because it is the wrong approach for code like this.

Ari Maniatis


-- 

-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A