我有一个JPA实体的类层次结构,所有这些实体都是从BaseEntity类继承的:
@MappedSuperclass
@EntityListeners( { ValidatorListener.class })
public abstract class BaseEntity implements Serializable {
// other stuff
}我希望在持久化和/或更新时自动验证实现给定接口的所有实体。这是我得到的。
我的ValidatorListener:
public class ValidatorListener {
private enum Type {
PERSIST, UPDATE
}
@PrePersist
public void checkPersist(final Object entity) {
if (entity instanceof Validateable) {
this.check((Validateable) entity, Type.PERSIST);
}
}
@PreUpdate
public void checkUpdate(final Object entity) {
if (entity instanceof Validateable) {
this.check((Validateable) entity, Type.UPDATE);
}
}
private void check(final Validateable entity, final Type persist) {
switch (persist) {
case PERSIST:
if (entity instanceof Persist) {
((Persist) entity).persist();
}
if (entity instanceof PersistOrUpdate) {
((PersistOrUpdate) entity).persistOrUpdate();
}
break;
case UPDATE:
if (entity instanceof Update) {
((Update) entity).update();
}
if (entity instanceof PersistOrUpdate) {
((PersistOrUpdate) entity).persistOrUpdate();
}
break;
default:
break;
}
}
}下面是它检查的Validateable接口(外部接口只是一个标记,内部包含方法):
public interface Validateable {
interface Persist extends Validateable {
void persist();
}
interface PersistOrUpdate extends Validateable {
void persistOrUpdate();
}
interface Update extends Validateable {
void update();
}
}所有这些都是可行的,但是我想将这种行为扩展到可嵌入的类。我知道两种解决办法:
public void persistOrUpdate(){ //首先//验证我自己的属性: myEmbeddable.persistOrUpdate();//这是可行的,但是我希望不需要手动调用}
发布于 2010-06-01 19:17:30
考虑基于注释的方法。它将产生更少的代码(似乎),而且几乎总是更容易理解。
引入新的注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Validators {
String[] values();
}将此注释应用于需要验证的每个实体和可嵌入对象,例如:
@MappedSuperclass
@EntityListeners( { ValidatorListener.class })
@Validators({Type.PERSIST, Type.UPDATE})
public abstract class MyEntity extends BaseEntity implements Serializable, Validateable {
// other stuff
@Validators(Type.PERSIST)
@Embedded
public Address getAddress() {
return address;
}
}当然,每个实体和可嵌入对象仍然应该实现变得更简单的Validateable接口:
public interface Validateable {
void validate(Type type);
}然后验证逻辑变得更简单:
如果实体被@Validators;
Validateable;
Validateable;Validators,但没有实现Validatable interface')validate对应于侦听器;这种方法允许您将对实体及其可嵌入元素(注释)的声明验证与验证逻辑(Java类-实体和可嵌入类)分开。例如,有时可嵌入的对象可能实现Validateable,但不需要验证。似乎听众也变得更简单了。
但是,如果您不是在将验证声明与验证逻辑分离之后,那么您的解决方案是相当令人满意的,并且可能更简单。
发布于 2010-06-01 10:18:58
好吧,这是我自己的解决方案:
http://pastebin.com/YPmt7ifm
我使用spring BeanUtils来迭代属性。还有:有人有一个更优雅的解决方案吗?
https://stackoverflow.com/questions/2948578
复制相似问题