JBoss, PostgreSQL, XDoclet, Serial primary keys in CMP

R

Richard Corfield

I'm trying to get a simple container managed bean to work with JBoss
3.2, PostgreSQL and Container Managed Persistence. I thought it would be
a good learning excercise. I wonder what techniques other people on this
newsgroup prefer for data access.

I've been impressed with XDoclet so far, as it has made things quite easy,
if only things worked.

I've tried a few variations:

Using the bean level tag:

* @jboss.unknown-pk
* class="java.lang.Long"
* readonly="true"
* column-name="reminder_id"
* auto-increment="true"

with an ID field defined as

/**
* @ejb.pk-field
*
* @ejb.persistence
* column-name="reminder_id"
*
* <!-- This should be for granted as its a pk-field? -->
* @ejb.value-object
* match="light"
*/
public abstract Long getID();
public abstract void setID(Long lID);

and manually creating the table with a SERIAL column for reminder_id.
Unfortunately when I run ejbCreate, I get a Not-Null constraint
violation on reminder_id. Presumably JBoss is setting that column to
NULL on the INSERT clause.

Another one from something I read on the JBoss pages was to define a
primary key of type Object. This doesn't help me refer to things by
primary key, for example in an Edit command on a web page - edit.do?id=123.


* @ejb.pk
* class="java.lang.Object"
* generate="false"
* @jboss.unknown-pk
* class="java.lang.Object"
* column-name="reminder_id"
* auto-increment="true"

and no @ejb.pk-fields.

This time its trying to recreate the table, and failing because Postgres
doesn't have an IDENTITY type. Changing back to Hypersonic gets closer,
telling me that IDENTITY and VARCHAR(32) don't mix, a change of SQL
data type in the @ejb.pk tag perhaps, but I'm moving further away from
numeric keys and my choice of database.

CMP beans and XDoclet seem to be a quite easy way (if it worked, or
assuming you know what you're doing) of getting a data access layer.
I've also looked at Hibernate, but wonder about my Hibernate classes
containing what are really Hibernate implementations of List and Set
being returned to the presentation layer. Of course I can avoid using
relationships in Hibernate, and I have a quick way of getting where I
was with Data Access Object, but with added features such as versioning.

Using Hibernate as part of the Model in a Struts based web page looks
worth exploring though, though I'd have to explore how I look after the
Session object and manage commit and disposal. I've used Command Object
pattern to deal with JDBC transactions in my fat client work.

I've used custom Data Access Objects encapsulating JDBC calls in my last
two major Java projects, and wondered if there was something easier.

Thanks
- Richard
 
R

Richard Corfield

Getting further, I've found the standardjbosscmp-jdbc.xml file in the
conf directory. This gives database mappings and <entity-command>s for
key generation. My task tonight perhaps, to either configure this for
postgresql (best), or use XDoclet's merge feature to add appropriate
entries to my jbosscmp-jdbc.xml. I notice that it supports JDBC3, so I
should be able to use that feature of the new Postgres driver.

- Richard
 
R

Richard Corfield

Getting further, I've found the standardjbosscmp-jdbc.xml file in the
conf directory. This gives database mappings and <entity-command>s for
key generation. My task tonight perhaps, to either configure this for
postgresql (best), or use XDoclet's merge feature to add appropriate
entries to my jbosscmp-jdbc.xml. I notice that it supports JDBC3, so I
should be able to use that feature of the new Postgres driver.

Getting even further. It's creating the rows, and they're being comitted
(must look at that transaction isolation) but is failing to serialise
the value object back to my test client as its primary key field of
type Object is not serializable of course. A task for later, but now
off to work.

The Value Object comes from XDoclet, and I'd like to stick to using that,
but I've hand hacked the jbosscmp-jdbc.xml to contain

<unknown-pk>
<unknown-pk-class>java.lang.Long</unknown-pk-class>
<jdbc-type>INTEGER</jdbc-type>
<sql-type>SERIAL</sql-type>
<auto-increment/>
</unknown-pk>

and JBoss' standardjbosscmp-jdbc.xml has

<defaults>
<datasource>java:/DefaultDS</datasource>
<datasource-mapping>PostgreSQL 7.2</datasource-mapping>

... as before ...

<unknown-pk>
<unknown-pk-class>java.lang.Long</unknown-pk-class>
<jdbc-type>INTEGER</jdbc-type>
<sql-type>SERIAL</sql-type>
<auto-increment/>
</unknown-pk>
<entity-command name="postgresql-fetch-seq"/>
<!-- Tried JDBC3, but it didn't work! Postgres reports
not implemented yet. -->

</defaults>

If I can have it use that unknown-pk from defaults, then I can hopefuly
use an XDoclet generated unknown-pk without resorting to patching or
merge files. It would also make sense to put this info in defaults, so
if I change database later, I don't need to change all my packaged
beans.

- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top