views:

55

answers:

1

Given:

@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="postType", discriminatorType=DiscriminatorType.STRING)
public class Post {}

@DiscriminatorValue(value = PostType.BUG)
public class BugReport extends Post {}

That is ... Bugs start life in this system as a Post. Later, they can be promoted(?) to being a BugReport.

I'm using the following approach to update this:

@Transactional
public Post setPostType(Post post, String postType)
{
    SQLQuery sql = getSession().createSQLQuery("UPDATE post SET postType = :postType where id = :id");
    sql.setString("postType", postType);
    sql.setInteger("id", post.getId());
    sql.executeUpdate();

    getSession().evict(post);
    getSession().flush();

    Post newPost = get(post.getId());
    return newPost;
}

Although this works, (the post type is changed, and returned from the db as a BugReport). I'm curious if there are any side effects or risks associated with this approach.

+1  A: 

Although this works, (the post type is changed, and returned from the db as a BugReport). I'm curious if there are any side effects or risks associated with this approach.

One problem I can see is that you are bypassing optimistic locking checks and a concurrent transaction updating the same Post could thus override the changes. So you should probably include the version check in your update and throw an exception if the count returned by executeUpdate is not 1.

Pascal Thivent