JBoss, PostgreSQL, XDoclet, Serial primary keys in CMP

Discussion in 'Java' started by Richard Corfield, Apr 21, 2004.

  1. 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

    --
    _/_/_/ _/_/_/ _/_/_/ Richard dot Corfield at ntlworld dot com
    _/ _/ _/ _/
    _/_/ _/ _/ Time is a one way street,
    _/ _/ _/_/ _/_/_/ Except in the Twilight Zone.
    Richard Corfield, Apr 21, 2004
    #1
    1. Advertising

  2. 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

    --
    _/_/_/ _/_/_/ _/_/_/ Richard dot Corfield at ntlworld dot com
    _/ _/ _/ _/
    _/_/ _/ _/ Time is a one way street,
    _/ _/ _/_/ _/_/_/ Except in the Twilight Zone.
    Richard Corfield, Apr 22, 2004
    #2
    1. Advertising

  3. On 2004-04-22, Richard Corfield <> wrote:
    > 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

    --
    _/_/_/ _/_/_/ _/_/_/ Richard dot Corfield at ntlworld dot com
    _/ _/ _/ _/
    _/_/ _/ _/ Time is a one way street,
    _/ _/ _/_/ _/_/_/ Except in the Twilight Zone.
    Richard Corfield, Apr 22, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. denis
    Replies:
    0
    Views:
    331
    denis
    May 24, 2004
  2. Andrea Sansottera

    no cmp field defined in cmp ejb

    Andrea Sansottera, Jul 16, 2004, in forum: Java
    Replies:
    0
    Views:
    373
    Andrea Sansottera
    Jul 16, 2004
  3. tmaus
    Replies:
    1
    Views:
    509
    Richard Hardy
    Oct 2, 2004
  4. Greg
    Replies:
    0
    Views:
    614
  5. Harry George
    Replies:
    9
    Views:
    684
    sonal
    Jun 13, 2006
Loading...

Share This Page