first of all thanks for everyone who tries to help me with this: I'm trying to validate a list with at least one element, if I check using
Set<ConstraintViolation<Object>> constraintViolations = VALIDATOR.validate(anObj);
The constraint works fine, but when I try to persist, the object with one element pass again and the list is null...
This is the part of the class with the list:
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;
//...
}
The validator:
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;
}
}
Annotation:
@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 {};
}
Test class:
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));
}
}
And finally, this is what happens when I run this
First of all, you can see the Team created with the list with one element.
Note: The helper creates a player and persist it first, then adds to a team and return it, and this is the Team that we can see.
In a second step, when it's trying to persist the element and gets through the validator, the list is empty... ¿why?
And finally, of course, with a null list, the merge method throws an exception:
Any idea of what could be happening? I don't know why this happens, as you can see, I commented the other lines, the test pass ok, but not when I try to update, it seems as another object, or as the object instantiates again
Thanks again to everyone
EDIT:
The getPlayer() and addPlayer() methods in Team object:
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;
}
Edit 2: DAO, as you can see, by now I don't need more specific methods in TeamDAO
public class TeamDAO extends AbstractDAOLayer<Team> {
}
UpdateEntity method in AbstractDAOLayer
public boolean updateEntity(T anObject){
try{
this.beginTransaction();
this.entityManager.merge(anObject);
this.finishtTransaction();
return true;
}
catch(Exception e){
return false;
}
}
Please check this. The problem has nothing to do with BV but rather with jpa merge semantic with many-to-many relationship.