java - Hibernate:与改变 DiscriminatorValue 相关的风险

标签 java hibernate orm jpa

给定:

@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 从数据库返回)。我很好奇这种方法是否有任何副作用或风险。

最佳答案

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.

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

关于java - Hibernate:与改变 DiscriminatorValue 相关的风险,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3723876/

相关文章:

java - WatchService/WatchEvent/事件路径

java - 打印字符串数组中的所有名称

java - 如何将 Streams Prev 元素结果传递给下一个

java - 获取JPA中实体的id而不从数据库中提取实体?

java - 如何更新 Spring WebFlow 模型中的数据并将其传递回同一 View

orm - ColdFusion ORM 在使用虚拟目录时找不到 CFC

java - 如何在 IBM Notes 中部署 java.policy 更改

java - 在 hibernate 中使用可序列化我使用什么底层数据库类型

java - org.hibernate.MappingException : Could not determine type for: java. util.List,在表 : College, 的列 : [org. hibernate.mapping.Column(students)]

javascript - 如何在对象数组中找到所有具有 ID 的记录?