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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]