【发布时间】:2013-05-23 08:50:33
【问题描述】:
我有以下场景:
@Entity public class Foo {
@Id private Long id;
@OneToMany(mappedBy = "foo")
@MyCustomConstraint
private Set<Bar> bars;
}
@Entity public class Bar {
// ...
@ManyToOne(optional = false)
Foo foo;
}
我有自动生成的代码(即不能修改它)创建一个新的Bar 并通过调用bar.setFoo(foo) 将其添加到现有的Foo。 setFoo 确保 bar 也被添加到 foo 的集合中。然后自动生成的代码调用persist(bar)。此时我需要运行foo.bars 上的自定义约束验证器(新添加的bar 可能会违反它),但事实并非如此。
我的问题:
- 这是设计使然还是我做错了什么?
- 我该怎么做才能让它发挥作用?
编辑:
有关自定义约束的更多信息 - 尽管我认为这与问题并不特别相关。
这只是一个javax.validation 自定义约束:
@javax.annotation.Constraint(validatedBy = MyCustomConstraintValidator.class)
@AllTheOtherAnnotationStuff
public @interface MyCustomAnnotation {
}
public class MyCustomConstraintValidator implements ConstraintValidator<MyCustomConstraint, Set /*<Bar>*/ .class> {
void initialize(MyCustomConstraint a) {}
boolean isValid(Set /*<Bar>*/ s, ConstraintValidatorContext ctx) { ... }
}
如果我调用entityManager.persist(foo),则验证约束。如果我调用entityManager.persist(bar),它不是,即使该栏是新添加到其Foo 的集合中。
【问题讨论】:
-
首先
mappedBy需要一个字段名(字符串)而不是一个类,这可能是一个错字。 -
它是,已修复。这是部分从记忆中重现的问题。
-
我不明白。您的自定义注释看起来如何?应该怎么处理?
-
这是一个 JSR-303/Hibernate 验证器吗?
-
这是一个 javax.persistence 验证器,我相信它是从 Hibernate 派生的,是的,JPA 实现是 Hibernate。