代码之家  ›  专栏  ›  技术社区  ›  Marty Pitt

休眠:与更改鉴别器值相关的风险

  •  2
  • Marty Pitt  · 技术社区  · 15 年前

    鉴于:

    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name="postType", discriminatorType=DiscriminatorType.STRING)
    public class Post {}
    
    @DiscriminatorValue(value = PostType.BUG)
    public class BugReport extends Post {}
    

    那是…bug在这个系统中作为一个帖子开始生命。以后可以升职(?)成为一个错误报告。

    我使用以下方法更新此:

    @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;
    }
    

    尽管这样做有效,(post类型被更改,并作为 BugReport )我很好奇这种方法是否有任何副作用或风险。

    1 回复  |  直到 15 年前
        1
  •  1
  •   Pascal Thivent    15 年前

    Post executeUpdate 1