For examples you can check Cayenne source code, specifically "org.apache.cayenne.dba" package and database specific subpackages such as "org.apache.cayenne.dba.oracle", etc.

Andrus


On Jan 3, 2008, at 4:22 AM, Terrence A. Pietrondi wrote:

Thanks Andrus. I'll accept everything you've mentioned
about the primary key generator, now just need a
simple example to follow and test it =). I searched
through the mailing list today, and it seems that an
Oracle key generator that simply increments the max by
one is very popular, but I didn't find any examples
(code). The methods 'generatePkForDbEntity' and
'reset' in the Javadoc don't detail what should be
done when implemented.

Any suggestions or URLS?

Example on installing it as well will be helpful since
I am unclear on your notes below.

Thanks.

--- Andrus Adamchik <[EMAIL PROTECTED]> wrote:

Hi Terrence,

Sorry for not replying earlier ... lots of things on
Cayenne front
plus the holidays :-)

Re: ObjectId question:

Putting PK values in the map returned by
'getReplacementIdMap' is
guaranteed to get picked up by Cayenne on commit and
processed
properly. If you change the ObjectId by hand, the
behavior is
unpredictable, as the ObjectId is used as a key in
the internal cache
maps. In fact it may vary between different versions
of Cayenne (I
think we did catch manual ObjectId changes before,
but I can't locate
this code in 3.0 anymore... may still be there, just
that I forgot
where it was).

But an official way to install a custom PkGenerator
is by implementing
your own org.apache.cayenne.dba.PkGenerator. The
generator should
only implement the 'generatePkForDbEntity' and
'reset' methods. The
rest can simply throw
"UnsupportedOperationException" as they are not
called in runtime. There are a few ways to install
custom PkGenerator:

* Cast runtime DbAdapter to JdbcAdapter or
AutoAdapter as appropriate
and call "setPkGenerator()"
* Subclass OracleAdapter, overriding
'JdbcAdapter.createPkGenerator()'
method, and entering the name of your adapter for
DataNode in the
Modeler.

As Ari mentioned, your solution (as well as
'getReplacementIdMap'
solution) has a race condition (that may or may not
be relevant in
your environment). Consider that between the time
you obtain a PK with
a query and a commit, another thread/process can
attempt an insert and
get the same id. As a result one of the two commits
will fail with a
duplicate PK SQL exception (or much worse - go
through if the
constraint is not defined on oracle, resulting in
corrupt data).

PkGenerator solution is less prone to a race
condition, as it is done
in the same JDBC transaction as the main commit,
however it also
depends on how you implement it (SELECT ... FOR
UPDATE may be needed
here).

Hope this helps.
Andrus



On Dec 27, 2007, at 4:46 PM, Terrence A. Pietrondi
wrote:

Any feedback on this topic? I read the mentioned
"Handling Inheritance" article, if I shouldn't use
the
table names, how can I check for my category
concepts
as I mentioned? Since there are multiple tables
that
are the categories.

I am a little unclear how using
ObjectId.getReplacementIdMap().put(...) might help
as
mentioned in an earlier post. Is there any other
information I can better provide to better explain
my
approach. How is this safer then using my own
ObjectId
object and settings its id?




Thanks.

--- "Terrence A. Pietrondi"
<[EMAIL PROTECTED]>
wrote:


--- Aristedes Maniatis <[EMAIL PROTECTED]> wrote:

The simplest way to have Cayenne handle PK
generation is to follow the
instructions here:





http://cayenne.apache.org/doc/generate-primary-key-support.html

That will implement a solution which is thread
safe
and portable
between databases. I don't believe your solution
would be thread safe.


Here is the trick, the work I am doing is on an
existing database (Oracle) and I do not have
access
to
modify the existing schema. It could be possible
that
I go through a review with our database
developer,
but
I am trying to solve the problem within the
constrains
of his design. For whatever reason, he chose not
to
implement auto-key generation and is expecting
that
to
take place in the application code. Whether that
is
a
good design decision or not is arguable, but I'd
like
to continue the implementation of my solution
without
database modifications. And therefore, key
generation
is going to have to take place in the application
code. Hope that is not too confusing.

What is it you are wanting to do here? One of
the
primary purposes of
an ORM is to insulate the user from the database
schema, allowing you
to refactor the database naming and schema
without
touching your Java
code. Hardcoding in database field names may not
be
in the best
interests of this approach. Also the comments in
your example suggest
a one to one mapping between databases tables
(DbEntity) and
ObjEntity. This is not always true in Cayenne:





http://cayenne.apache.org/doc/handling-inheritance.html


I have a service that has a method to set a value
for
an entry of a member on a table. This method is
"annotating" a "member" of a "category"
(conceptually). And so, when using the "annotate"
method, of the arguments for this method are a
"rule"
and a "value". A "rule" is a column on the
database
table, and a table represents a "category". I
need
to
map this "rule" to the correct property name in
Cayenne so I can set it properly for the correct
"member". To be clear, I am editing an existing
entry
in the table, but I am targeting a specific
column
to
set, and I need to map from the column name to
the
property name.

Thanks.



Terrence A. Pietrondi
http://del.icio.us/tepietrondi






____________________________________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search.



http://tools.search.yahoo.com/newsearch/category.php?category=shopping



Terrence A. Pietrondi
http://del.icio.us/tepietrondi





____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile.  Try it now.

http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ






Terrence A. Pietrondi
http://del.icio.us/tepietrondi


____________________________________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping


Reply via email to