【发布时间】:2014-10-06 07:26:34
【问题描述】:
我正在考虑实现一个反模式,因为 @EntityListeners 在某些情况下是不够的:
@MappedSuperclass
public abstract class AbstractEntity implements Serializable
{
...
public abstract AbstractEntity save(EntityManager em);
...
}
@Entity
public class ConcreteEntity extends AbstractEntity
{
...
public ConcreteEntity save(EntityManager em)
{
doSomeStuff(this);
ConcreteEntity merged;
if(id == null)
{
em.persist(this);
merged = this;
}
else
{
merged = em.merge(this);
}
doOtherStuff(merged);
return merged;
}
...
}
专业版:
- 具体业务逻辑在 Object 内部(REAL OO 编程)
- 利用继承来控制业务逻辑(另一种 OO 模式)
- 可以编写通用 EJB
缺点:
- 级联不调用
- 合约添加:禁止调用
em.persist(entity)/em.merge(entity)
还有什么我忘记了吗?
【问题讨论】:
-
不只是
ActiveRecord模式扩展为包括监听器(@PreInsert等)的东西吗? -
不知道这个。是的,它是:)
标签: jpa anti-patterns