Play - Deleting a post by id using Ebean permanantly

喜你入骨 提交于 2019-12-12 04:55:07

问题


I am implementing a delete option for a post! I have got model for a post and a deletePostOnly() java method for it to delete it PERMANENTLY using ebean delete() function.

But the problem is that it is doing nothing and it is not deleting the post. Following is the picture of the sample post.

delete post java method

public static Result deletePostOnly(Long postId) {

    //check if post can be deleted with this user
    SimplePost post = SimplePost.find.byId(postId);
    if(post == null) {
        return badRequest();
    }

    UserAccount account = Secured.getCurrentUser();
    if(!(post.getPostUserId().equals(account.getId()))) {
        return badRequest();
    }

    try {
        post.delete();        
        post.save();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return ok("ok");
}

This is the delete() function it is calling on

public void delete() {
    Ebean.delete(this);
}

This is save() function

@Override
public void save() {
    super.save();
}

I have also tried using creating ebean query, but it is very inconsistent (it works sometimes & it doesn't sometimes.

Can anyone please tell me where I am going wrong or may be another different way to delete this post more efficiently? I couldn't think of it!

Any help/suggestions much welcomed!


回答1:


If you save your bean after having deleted it, you basically recreate it. Leave out the post.save() line.



来源:https://stackoverflow.com/questions/28688030/play-deleting-a-post-by-id-using-ebean-permanantly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!