首先感谢所有试图帮助我的人:我正在尝试使用至少一个元素验证列表,如果我使用
Set<ConstraintViolation<Object>> constraintViolations = VALIDATOR.validate(anObj);
约束工作正常,但是当我尝试持久化时,带有一个元素的对象再次传递,列表为空...
这是包含列表的类的一部分:
public class Team implements Serializable {
//...
@AtLeastOneNotNull
@ManyToMany
@JoinTable(
name="teams_players"
, joinColumns={
@JoinColumn(name="id_team")
}
, inverseJoinColumns={
@JoinColumn(name="id_player")
}
)
private List<Player> players;
//...
}
验证员:
public class AtLeastOneNotNullValidator implements ConstraintValidator<AtLeastOneNotNull, List<?>> {
@Override
public void initialize(AtLeastOneNotNull constraint) {
}
@Override
public boolean isValid(List<?> aCollection, ConstraintValidatorContext aConstraintValidatorContext) {
if(aCollection == null || aCollection.isEmpty())
return false;
else
return true;
}
}
注释:
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = AtLeastOneNotNullValidator.class)
public @interface AtLeastOneNotNull {
String message() default "{[ERROR] Collection must have at least one element}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
测试类:
public class TeamDAOTest {
private static Validator VALIDATOR;
TeamTestHelper teamTestHelper = null;
TeamDAO teamDaoTest = null;
@Before
public void initTestClass() {
teamDaoTest = new TeamDAO();
teamTestHelper = new TeamTestHelper();
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
VALIDATOR = factory.getValidator();
}
@After
public void endTestClass() {
}
@Test
public void savingRightTeam() {
Team testTeam = teamTestHelper.getTeamOK();
// Set<ConstraintViolation<Team>> constraintViolations = VALIDATOR.validate(testTeam);
//
// assertEquals(0, constraintViolations.size());
//
assertEquals("Inserting right Team", true, teamDaoTest.updateEntity(testTeam));
}
@Test
public void savingWrongTeam() {
Team testTeam = teamTestHelper.getTeamKO_WithoutPlayers();
// Set<ConstraintViolation<Team>> constraintViolations = VALIDATOR.validate(testTeam);
//
// assertEquals(1, constraintViolations.size());
assertEquals("Inserting empty Team", false, teamDaoTest.updateEntity(testTeam));
}
}
最后,当我运行这个
时会发生这种情况首先,您可以看到使用包含一个元素的列表创建的团队。 注意:帮助者创建一个玩家并先保留它,然后添加到一个团队并返回它,这就是我们可以看到的团队。
在第二步中,当它试图保持元素并通过验证器时,列表为空......¿为什么?
最后,当然,使用空列表,merge方法会抛出异常:
知道会发生什么事吗?我不知道为什么会发生这种情况,你可以看到,我评论了其他行,测试通过了ok,但是当我尝试更新时,它似乎是另一个对象,或者当对象再次实例化时
再次感谢大家
编辑:
Team对象中的getPlayer()和addPlayer()方法:
public List<Player> getPlayers() {
if(this.players == null)
this.players = new ArrayList<Player>();
return this.players;
}
public boolean addPlayer(Player aPlayer){
if(!getPlayers().contains(aPlayer)){
this.players.add(aPlayer);
return true;
}
else return false;
}
编辑2:DAO,如您所见,到目前为止我在TeamDAO中不需要更具体的方法
public class TeamDAO extends AbstractDAOLayer<Team> {
}
AbstractDAOLayer中的UpdateEntity方法
public boolean updateEntity(T anObject){
try{
this.beginTransaction();
this.entityManager.merge(anObject);
this.finishtTransaction();
return true;
}
catch(Exception e){
return false;
}
}