You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Unnikrishnan <un...@indpro.se> on 2007/11/27 07:24:21 UTC

Retrieving last inserted ID

Hi 

I am new to Struts 2. I would like to know how to retrieve last inserted id.

I have  page called "welcome" where I have 3 fields (name, telephone, email) 
my execute method is as follows. 

 public String execute() throws Exception {
        CustomerFacadeLocal customerFacedeLocalObj= lookupCustomerFacade();
        Customer customerObj = new Customer();
        customerObj.setName(getName());
        customerObj.setTelephone(getTelephone());
        customerObj.setEmail(getEmail());
        customerFacedeLocalObj.create(customerObj);
        return SUCCESS;
}

So after this insertion happens , i need to take the last inserted id.

Please help 

Thanks in advance
Unni

-- 
View this message in context: http://www.nabble.com/Retrieving-last-inserted-ID-tf4880006.html#a13965438
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Retrieving last inserted ID

Posted by Omkar Patil <os...@gmail.com>.
Unni,

I'm not sure if this is related to Struts2. CustomerFacadeLocal looks like
an application specific class for your application. You'll need to find out
what api this class provides within your application

- Omkar


Unnikrishnan wrote:
> 
> Hi 
> 
> I am new to Struts 2. I would like to know how to retrieve last inserted
> id.
> 
> I have  page called "welcome" where I have 3 fields (name, telephone,
> email)  my execute method is as follows. 
> 
>  public String execute() throws Exception {
>         CustomerFacadeLocal customerFacedeLocalObj=
> lookupCustomerFacade();
>         Customer customerObj = new Customer();
>         customerObj.setName(getName());
>         customerObj.setTelephone(getTelephone());
>         customerObj.setEmail(getEmail());
>         customerFacedeLocalObj.create(customerObj);
>         return SUCCESS;
> }
> 
> So after this insertion happens , i need to take the last inserted id.
> 
> Please help 
> 
> Thanks in advance
> Unni
> 
> 

-- 
View this message in context: http://www.nabble.com/Retrieving-last-inserted-ID-tf4880006.html#a13966734
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Retrieving last inserted ID

Posted by Gary Affonso <gl...@greywether.com>.
Unnikrishnan wrote:
> Hi 
> 
> I am new to Struts 2. I would like to know how to retrieve last inserted id.
> 
> I have  page called "welcome" where I have 3 fields (name, telephone, email) 
> my execute method is as follows. 
> 
>  public String execute() throws Exception {
>         CustomerFacadeLocal customerFacedeLocalObj= lookupCustomerFacade();
>         Customer customerObj = new Customer();
>         customerObj.setName(getName());
>         customerObj.setTelephone(getTelephone());
>         customerObj.setEmail(getEmail());
>         customerFacedeLocalObj.create(customerObj);
>         return SUCCESS;
> }
> 
> So after this insertion happens , i need to take the last inserted id.

This has little to do with S2, it has to do with your DAO layer (in your 
case your CustomerFacade).

If it were me, I'd probably change the create() method to return the ID 
of the inserted object.  So the last lines would read something like this...


     ...
     Long newId = customerFacedeLocalObj.create(customerObj);
     return SUCCESS;
   }

Now the work of retrieving the Id is inside the create() method.  Still 
doesn't answer how to do the work of getting the last-inserted-id, though.

And that answer is typically database specific (and going to be effected 
by other dao-middleware like hibernate).  You do it one way with 
JDBC/MySQL.  You do it a completely different way with Hibernate/Oracle.

You should find some example code for grabbing the ID if you google a bit.

- Gary

P.S.  And let me add that you could completely do away with the facade 
lookup if you were using Spring.

P.P.S.  And let me also add that S2 could be directly injecting the 
name, telephone, and email data right your customerObj without having to 
call 3 additional "set" methods (or even create the customerObj at all). 
  Checkout the docs for ModelDriven and read some of the posts about 
direct-model-injection.

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