Re: Smartest way to realize sequence PID

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Thu Oct 14 2004 - 10:42:16 EDT

  • Next message: Michal Kozlowski: "Multiple database strangeness"

    Hi Ken,

    You didn't mention if "id" is a database primary key of TUser. Is it? If
    so you can simply rely on Cayenne default PK generation mechanism
    (http://objectstyle.org/cayenne/userguide/design/autopk.html). You don't
    really need to do anything special for it, it will just work.

    If not, then the best idea IMO would still be to take advantage of Cayenne
    PkGenerator, though I need to understand your requirements before I can
    comment on that further.

    Andrus

    > Hi every one.
    > I want your advice to realize sequenced primary id.
    > (like SERIAL data type in Postgres)
    >
    > Follow is my idea.But I think it is not samrt way.
    > It use many memory space and CPU power.
    > -------------------------------------------------------
    > public class TUser extends _TUser {
    >
    > public Integer nextId() {
    > int maxId = 0;
    >
    > DataContext ctx = DataContext.createDataContext();
    > SelectQuery query = new SelectQuery(TUser.class);
    > List list = ctx.performQuery(query);
    >
    > Iterator i = list.iterator();
    > while(i.hasNext()) {
    > TUser user = (TUser)i.next();
    >
    > int tempId = user.getId().intValue();
    >
    > if(tempId > maxId){
    > maxId = tempId;
    > }
    > }
    >
    > return new Integer(maxId + 1);
    > }
    > }
    > --------------------------------------------------------------
    > //insert the data
    > tUser.setId(tUser.nextId());
    > tUser.setAccount(userForm.getAccount());
    > tUser.setPass(userForm.getPass());
    > ctx.commitChanges(org.apache.log4j.Level.WARN);
    > --------------------------------------------------------------
    >
    > If some one has realized that. Please tell me the way to realize.
    >
    > Ken Yasue.



    This archive was generated by hypermail 2.0.0 : Thu Oct 14 2004 - 10:42:18 EDT