Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: setting a default ID with BC4J Entity

Re: setting a default ID with BC4J Entity

From: Karsten Farrell <kfarrell_at_belgariad.com>
Date: Wed, 13 Nov 2002 21:33:44 GMT
Message-ID: <YyzA9.89$xA3.13453518@newssvr21.news.prodigy.com>


Owen Gibbins wrote:
> Hello all,
>
> I'm trying to customize an Entity object in order to set the ID column of a
> newly created row to a unique number. I'm overriding the create method of
> the object. I managed to get it to work using the following:
>
> protected void create(AttributeList nameValuePair)
> {
> super.create(nameValuePair);
> final String stmt = "SELECT MAX(ID) + 1 FROM PRODUCER";
> Number id = new Number(0);
> ViewObject idView =
> getDBTransaction().createViewObjectFromQueryStmt(stmt);
> Row idRow = idView.next();
> if(idRow != null)
> {
> Object o = idRow.getAttribute(0);
> if(o != null)
> {
> if(o instanceof Number)
> id = (Number)o;
> else
> throw new Error("ProducerImpl.create: \"" + stmt + "\" did not
> produce type NUMBER.");
> }
> }
> setID(id);
> }
>
> The problem with this is that the SELECT statement only considers rows that
> have been posted to the database. I need the other newly created rows in the
> current transaction to be considered as well. So I tried the following:
>
> protected void create(AttributeList nameValuePair)
> {
> super.create(nameValuePair);
> ViewObject view = getDBTransaction().createViewObject("ProducerView");
> view.setOrderByClause("ID DESC");
> view.executeQuery();
> Number id;
> ProducerViewRow row = (ProducerViewRow)view.next();
> if(row != null)
> id = row.getID();
> else
> id = new Number(0);
> setID(id);
> }
>
> The createViewObject() throws an exception. So I suppose my question is: how
> do I create a view object from within an Entity method?
>
> Thanks for any help!
>
> Owen
>
>

Couple of things:

  1. Issue the following (or similar) statement in sqlplus:

SQL> create sequence producer_seq start with 1 increment by 1 cache 10;

2. Change the SELECT statement in your create method:

final String stmt = "SELECT producer_seq.nextval FROM DUAL";

Now you'll always be guaranteed a unique number from the 'nextval' clause ... even if multiple people request one and don't commit their transaction right away. Received on Wed Nov 13 2002 - 15:33:44 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US