- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个“架构”的“发布”实体/表:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer postId;
private Integer revisionId;
private Boolean isCurrentRevision;
因此,表格包含帖子,每个帖子都有多个修订版,但只有其中一个(对于每个帖子)是最新的。
现在,假设我想保留现有帖子的另一个修订版(即更新帖子):
我需要为这个 postId 找到最高的现有 revisionId,增加它并将它设置为 revisionId。此外,这是新的当前修订版,因此应相应地进行标记,但以前的当前修订版也应取消标记。
但是我该怎么做呢?我觉得这真的应该是实体实现的一部分,但另一方面我需要 EntityManager 来做到这一点。但是我找不到注入(inject) EntityManager 实例的方法(保证存在)。
这可能吗?你如何实现这样的场景?谢谢!
最佳答案
不幸的是,对于您提出的建议,没有干净、便携的方法。 JPA 2 规范 ( JSR 317 ) 声明如下:
In general, the lifecycle method of a portable application should not invoke EntityManager or Query operations, access other entity instances, or modify relationships within the same persistence context.
就实现而言,Hibernate 明确禁止它:
A callback method must not invoke EntityManager or Query methods!
我知道这不是您真正希望的,但看来您必须将修订管理责任分配给实体类的客户。
关于java - 如何使用@PrePersist 的 EntityManager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5267408/
我在继承和 方面遇到了一些问题@PrePersist 注解。 我的源代码如下所示: _带有带注释的 updateDates() 方法的“基”类: @javax.persistence.Entity @
我有 GenericEntity 类,其中包含 @PrePersist 方法 onCreate(),它在将实体保存到数据库之前设置时间戳。它运行良好,但当我尝试测试我的 DAO 类时,我遇到了问题。
我是 Symfony2 的新手,我想知道 prePersist 和 preUpdate 事件有什么区别。看起来 prePersist 在我“保留”记录之前被“触发”,但是 preUpdate 什么时候
我如何模拟 @PrePersist 方法,例如我实例化的实体的 preInit() ? 我正在使用 TestNG。 EasyMock 是首选。 @Test(enabled = true) public
我的实体的 prePersist() 方法没有被调用。 username = $username; return $this; } /** * Get use
我用这段线来合并: partner = em.merge(partner); 这是在我的实体中: @PrePersist public void updateModificationDate(){
我有以下用于将用户存储在数据库中的实体,目前它仍然是 WIP,但我在创建新用户时遇到重复问题。每次我使用注册表单和 prePersist 添加新用户时,它都会复制 Roles 表中的值。在 addUs
我正在使用 Hibernate 和 MySQL 在 Spring MVC 中开发一个应用程序,但我遇到了一个问题。我正在尝试使用 @PrePersist 注释在我的 Java 实体中填充我最后修改的字
我在实现 Doctrine EventListener 时遇到问题。创建新发票时,InvoiceType 表单中包含一组项目(标题、价格、金额)。对于发票,在 price 字段中,我想插入所有购买产品
假设以下代码片段使用 @PrePersist 和 @PreUpdate 注释和 Joined-type 继承: @Entity @Inheritance(strategy=InheritanceTyp
根据doctrine documentary : The prePersist event occurs for a given entity before the respective Entity
我的 Symfony2 项目中有一个 Doctrine2 实体(称之为实体 A)。该实体与项目中的另一个实体(称为实体 B)具有 ManyToOne 关系。 实体 A 的状态属性为“事件”或“非事件”
根据doctrine documentary : The prePersist event occurs for a given entity before the respective Entity
如果未手动设置,我想自动设置其中一个实体字段。有没有办法检查这个?这些字段有一个默认值,所以我不能简单地比较这个值。我想知道学说是否维持值(value)是否改变以及我是否可以访问该信息。 此外,Doc
我编写了一个 @PrePersist 方法,该方法在保留我的 User 对象之前调用。该方法的目的是为用户进行一种预订,因此其他人无法获取他的电子邮件地址。因此,当用户即将被保留时,我说,“为用户#x
我有这个“架构”的“发布”实体/表: @Id @GeneratedValue(strategy = GenerationType.AUTO) int id; @GeneratedValue(strat
我有一个事务性警报表和一个主警报类型表。我想在表中添加警报时发送一封电子邮件,所以我想我会使用 PrePersist。但是,在我的电子邮件中,我想包含一些包含在警报类型表中的信息。 我试图在 Aler
我的代码 @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class SiteMessage imple
我正在使用基本实体: @MappedSuperclass public class BaseEntity { private static final Logger L = LoggerFac
好吧,我是 symfony 的初学者,到目前为止,我开发了一个网站,用户可以在其中注册自己,并在登录后动态创建表单,但我的注册表单无法按预期工作。我的基本想法是当用户尝试注册自己时,预先检查他输入的客
我是一名优秀的程序员,十分优秀!