You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-user@db.apache.org by Kintzel Levente <ki...@softech.ro> on 2005/02/23 14:09:22 UTC

Autoincrement


Hi!

I have a table with an id field autoincrement and an another field (numb  
for example). The table name is Constants. Now I add a row in this table:

   Constants ct=new Constants();
   ct.setNumb(9734);
   ct.save();
   System.out.println(ct.getId()); //returns always 0


In the table ct will have associated the auto generated ID too. How can I  
obtain its value?


Criteria cr=new Criteria();
cr.add(ConstantsPeer.NUMBER,9734);
List lst=ConstantsPeer.doSelect(cr);
ct=(Constants) lst(0);
System.out.println(ct.getId()); //returns the correct id

There exist a simpler solution? If the Number field is not unique then I  
have no guarantee that I obtain the correct value...

An another question... If I use the Id field as a foreign Key in another  
table:

     ConstantsDescr (Id, Description) //Id foreign key that refer  
Constants(Id)

and I want to add a new record in this table like this:

   Constants ct=new Constants();
   ct.setNumb(9734);
   ConstantsDescr cdesc=new ConstantsDescr();
   cdesc.setDescription("I don't know!');
   ct.addConstantsDescr(cdesc) ;
   ct.save();

I will receive an error that the foreign key constraint was violated.  
Somebody can show me a simple solution for this. Thank you!

   Levy


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


RE: Peer Class and associated fun

Posted by Thomas Fischer <fi...@seitenbau.net>.



Hi Brandon,

You can use the reflection API for that.
If you want to use a String as a name, you can use
Class.forName("your.target.package." + name).instance(), and set name to
"Book", you can get an instance of Book, and proceed like you do. You might
even be able to use Class.forName("your.target.package." + name
+"Peer").getMethod(...) to execute the doSelect of the peer directly
without creating an instance of Book.

HOWEVER, personally I am not sure whether your approach makes sense. Each
time you use the reflection approach, you lose type safety. The deeper you
bury the generation in your framework, the more difficult is it to find the
cause for class cast exceptions and the like. Also, you cannot use IDE
features like "refactore" and "search references" any more, because your
binding is too loose.

Of course, you have introduced additional flexibility of the classes which
you can produce with one method. But In practice, I personally have never
missed such flexibility. Usually, I know that I want to read books in a
place, and therefore I can also write "BookPeer.doSelect()" instead of
"GeneralObjectRetriever.retrieve()". If this is not flexible enough for
you, you can try Torque's inheritance mechanism (no idea how good it works,
never used it myself). If this is still not flexible enough, well, then you
have to do something like you plan to do.

Again, this is just my personal experience and opinion, and might not at
all be applicable to your specific situation.

           Thomas

"Brandon" <ef...@hotmail.com> schrieb am 23.02.2005 20:27:28:

> In order to simply the Torque programming model even further, I am trying
to
> create CRUD methods for a database that will essentially do the stuff
that
> I'm requesting for me.  That way instead of writing all of this stuff for
a
> retrieve everytime, I just call a generic method retrieve() and it
returns
> what I wanted to select on from the database.  So I've noticed that all
of
> my auto-generated databases implement this interface called Persistent.
> This is essentially the way I see things going right now:
>
> retrieve (Persistent p)
> {
>     retrieve (p, null);
> }
>
>
> retrieve (Persistent p, Map m)
> {
>     // create a criteria here
>     // if m != null then go through a for loop that iterates through the
map
> and adds to
>     // the criteria object
>     return ((BaseObject)p).getPeer.doSelect(criteria);
> }
>
>
> This seems to simplify things a bit, but I think that passing in a String

> rather than a Persistent makes much more sense.  It just seems akward to
> program this way.  Using the example database the tutorial provides, in
> order to perform a statement equivalent to "select * from book;" one
would
> have to pass in a new Book object to retrieve.  Essentially the call
would
> look like "List bookList = retrieve(new Book());" and I think it makes
much
> more sense to do something like "List bookList = retrieve("Book");".
Does
> anyone know how I could accomplish something like this or am I stuck
passing
> in a new Book object?
>
> -Brandon
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org
>


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


Peer Class and associated fun

Posted by Brandon <ef...@hotmail.com>.
In order to simply the Torque programming model even further, I am trying to 
create CRUD methods for a database that will essentially do the stuff that 
I'm requesting for me.  That way instead of writing all of this stuff for a 
retrieve everytime, I just call a generic method retrieve() and it returns 
what I wanted to select on from the database.  So I've noticed that all of 
my auto-generated databases implement this interface called Persistent. 
This is essentially the way I see things going right now:

retrieve (Persistent p)
{
    retrieve (p, null);
}


retrieve (Persistent p, Map m)
{
    // create a criteria here
    // if m != null then go through a for loop that iterates through the map 
and adds to
    // the criteria object
    return ((BaseObject)p).getPeer.doSelect(criteria);
}


This seems to simplify things a bit, but I think that passing in a String 
rather than a Persistent makes much more sense.  It just seems akward to 
program this way.  Using the example database the tutorial provides, in 
order to perform a statement equivalent to "select * from book;" one would 
have to pass in a new Book object to retrieve.  Essentially the call would 
look like "List bookList = retrieve(new Book());" and I think it makes much 
more sense to do something like "List bookList = retrieve("Book");".  Does 
anyone know how I could accomplish something like this or am I stuck passing 
in a new Book object?

-Brandon 

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