ItEr21S04ArquitecturaServidorItEr20S04: Simplifying method save at CriterionService.

This commit is contained in:
Manuel Rego Casasnovas 2009-08-11 13:05:21 +02:00 committed by Óscar González Fernández
parent 5728751429
commit f56d0c2af4
4 changed files with 14 additions and 12 deletions

View file

@ -23,6 +23,12 @@ public class Criterion extends BaseEntity implements ICriterion {
private boolean active = true;
/*
* Just for Hibernate mapping in order to have an unique constraint with
* name and type properties.
*/
private Long typeId;
public static Criterion ofType(CriterionType type) {
return new Criterion(type);
}

View file

@ -6,7 +6,6 @@ import java.util.Date;
import java.util.List;
import org.apache.commons.lang.Validate;
import org.hibernate.validator.InvalidValue;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.resources.daos.CriterionDAO;
@ -79,16 +78,6 @@ public class CriterionServiceImpl implements ICriterionService {
}
criterionDAO.save(entity);
if (criterionDAO.findByNameAndType(entity).size() > 1) {
InvalidValue[] invalidValues = {
new InvalidValue(entity.getName() + " already exists",
Criterion.class, "name", entity.getName(), entity)
};
throw new ValidationException(invalidValues,
"Couldn't save new criterion");
}
}
private CriterionType saveCriterionType(CriterionType criterionType) throws ValidationException {

View file

@ -36,6 +36,13 @@
<property access="field" name="name"/>
<property access="field" name="active"/>
<many-to-one access="field" name="type" column="id_criterion_type" not-null="true" />
<properties name="nameAndType" unique="true">
<property access="field" name="name" insert="false" update="false"/>
<property access="field" name="typeId" insert="false" update="false"
column="id_criterion_type"/>
</properties>
</class>
<!-- CriterionSatisfaction -->

View file

@ -114,7 +114,7 @@ public class CriterionServiceTest {
criterionService.save(criterion);
}
@Test(expected=ValidationException.class)
@Test(expected = DataIntegrityViolationException.class)
@NotTransactional
public void testCannotExistTwoDifferentCriterionsWithSameNameAndType() throws ValidationException {
String unique = UUID.randomUUID().toString();