You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Da...@cec.eu.int on 2004/12/03 10:06:58 UTC

RE: How to obtain autoincrement field value once an object is cre ated?

To use the auto-increment, you must specify the sequence name.
I suggest that you get your sequence number manually, set your fields (pk
and the others)
with this value and store the object.
Normally, the auto-increment is not taken into account if you provide an id.
Can someone confirm the last point ?

David WIESZTAL.


-----Original Message-----
From: news [mailto:news@sea.gmane.org] On Behalf Of Gene
Sent: Friday, December 03, 2004 5:00 AM
To: ojb-user@db.apache.org
Subject: Re: How to obtain autoincrement field value once an object is
created?


But before I can store() the object, I must filled the TheSameValueField
 with values(same as Id field) as it is a not null field/column.
Otherwise, database error will be raised.

Gene



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: How to obtain autoincrement field value once an object is created?

Posted by Gene <ge...@yahoo.com>.
Alessandro Colantoni <acolantoni <at> brujulatelecom.com> writes:

> 
> Yes I confirm it.

Thank you. Could you tell me what about your ServiceLocator class?

Gene




---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


RE: How to obtain autoincrement field value once an object is created?

Posted by Alessandro Colantoni <ac...@brujulatelecom.com>.
Yes I confirm it.
I used this strategy a couple of times and it looks work.
The sequence is autoincremented just if the correspondent value you
store is null. 

If it can help, to retrieve the new value of the sequence before storing
I always use this class



public class SequenceDAO {
    // Create Log4j category instance for logging
    static private org.apache.log4j.Category log =
org.apache.log4j.Category.getInstance(SequenceDAO.class.getName());
    
    public SequenceDAO() {
    }
    public Object getSequence(ValueObject valueObject, String
fieldName)throws DataAccessException{
        log.info("************Entering the
SequenceDAO.getSequence(ValueObject valueObject, String
fieldName)***************");
        PersistenceBroker broker = null;
        Object sequence = null;
        try{   
            broker = ServiceLocator.getInstance().findBroker();
 
sequence=broker.serviceSequenceManager().getUniqueValue(broker.getClassD
escriptor(valueObject.getClass()).getFieldDescriptorByName(fieldName));
        } catch (SequenceManagerException e) {
            log.error("SequenceManagerException thrown in
SequenceDAO.getSequence(ValueObject valueObject, String fieldName): " +
e.toString());
            throw new DataAccessException("Error in
SequenceDAO.getSequence(ValueObject valueObject, String fieldName): " +
e.toString(),e);
        } catch (ServiceLocatorException e) {
            log.error("PersistenceBrokerException thrown in
SequenceDAO.getSequence(ValueObject valueObject, String fieldName): " +
e.toString());
            throw new DataAccessException("Error in
SequenceDAO.getSequence(ValueObject valueObject, String fieldName): " +
e.toString(),e);
        } finally {
                  if (broker != null) broker.close();
        }
        log.info("************Done with the
SequenceDAO.getSequence(ValueObject valueObject, String
fieldName)***************");
        return sequence;
    }
}


to use it just do

SquenceDAO sequenceDAO = new sequenceDAO();
mysquence = (MyType)getSequence(myValueObject, "myfieldname")

then you put mysequence in myfield of myValueObject and store.
It sould work

-----Mensaje original-----
De: David.WIESZTAL@cec.eu.int [mailto:David.WIESZTAL@cec.eu.int] 
Enviado el: viernes, 03 de diciembre de 2004 10:07
Para: ojb-user@db.apache.org
Asunto: RE: How to obtain autoincrement field value once an object is
created?

To use the auto-increment, you must specify the sequence name.
I suggest that you get your sequence number manually, set your fields
(pk
and the others)
with this value and store the object.
Normally, the auto-increment is not taken into account if you provide an
id.
Can someone confirm the last point ?

David WIESZTAL.


-----Original Message-----
From: news [mailto:news@sea.gmane.org] On Behalf Of Gene
Sent: Friday, December 03, 2004 5:00 AM
To: ojb-user@db.apache.org
Subject: Re: How to obtain autoincrement field value once an object is
created?


But before I can store() the object, I must filled the TheSameValueField
 with values(same as Id field) as it is a not null field/column.
Otherwise, database error will be raised.

Gene



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org