2010-09-16 52 views
2

鉴于有关的风险:休眠:不断变化的DiscriminatorValue

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

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

这是...错误在这个系统中作为后重新开始生活。之后,他们可以被提升(?)成为BugReport。

我用下面的方法来更新这个:

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

虽然这工作,(岗位类型改变,并从数据库返回的BugReport)。我很好奇,如果有任何副作用或与此方法相关的风险。

回答

1

虽然这可行,(帖子类型被更改,并从数据库返回作为BugReport)。我很好奇,如果有任何副作用或与此方法相关的风险。

我可以看到的一个问题是,你绕过乐观锁定检查和并发事务更新相同Post可以因此覆盖更改。因此,如果executeUpdate返回的计数不是1,则应该在更新中包含版本检查并引发异常。