Mitesh Meswani wrote:
Dave wrote:
On 1/18/07, Mitesh Meswani <[EMAIL PROTECTED]> wrote:
> if(object.id != null)
> update()
> else
> save()
This would be true if the id is generated as is the case with Roller.
For objects where id is user supplied, above algorithm can not be used.
Thats the reason some frameworks (for example JPA) does not provide
equivalent of saveOrUpdate() method.
Yes, that is true and it is the case with the property object, whose
ID is the name property.
And, we can't rely on object.id because we don't know which field is
the id. So, for JPA you'd have to do something like this in the store
method:
EntityManager em = getEntityManager(true);
if (!em.contains(obj)) {
// If entity is not managed we can assume it is new
em.persist(obj);
}
return obj;
Above has a performance penalty to pay and it assumes that no detached
object will be ever passed to store(). Currently, I don't think that is
true. For example, I found following with a quick search
Class MaintenanceAction {
....
public ActionForward flushCache(
ActionMapping mapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
try {
RollerRequest rreq = RollerRequest.getRollerRequest(request);
-----> WebsiteData website = rreq.getWebsite(); // I think this is a
detached object
RollerSession rses = RollerSession.getRollerSession(request);
if ( rses.isUserAuthorizedToAdmin(website) ) {
// some caches are based on weblog
last-modified, so update it
website.setLastModified(new Date());
try {
UserManager umgr =
RollerFactory.getRoller().getUserManager();
-----> umgr.saveWebsite(website); // The save will
not work as it will end up calling persist
Actually, that shouldn't be a detached object because the RollerRequest
class should be querying for the object on each request. In any case
though, the main point is as Dave suggested before, we will *NOT*
support the saving of detached objects anymore and anywhere that does it
now is considered a bug and needs to be fixed.
-- Allen
-Mitesh
- Dave