VersionMisuse extends FieldConstraint { @Override protected void check(MappedClass mc, MappedField mf, Set<ConstraintViolation> ve) { if (mf.hasAnnotation(Version.class)) { Class<?> type = mf.getType(); if (Long.class.equals(type) || long.class.equals(type)) { Object testInstance = DefaultCreator.createInst(mc.getClazz()); if (Long.class.equals(type)) { if (mf.getFieldValue(testInstance) != null) ve.add(new ConstraintViolation(Level.FATAL, mc, mf, this.getClass(), "When using @" + Version.class.getSimpleName() + " on a Long field, it must be initialized to null.")); } else if (long.class.equals(type)) { if ((Long) mf.getFieldValue(testInstance) != 0L) ve.add(new ConstraintViolation(Level.FATAL, mc, mf, this.getClass(), "When using @" + Version.class.getSimpleName() + " on a long field, it must be initialized to 0.")); } } else ve.add(new ConstraintViolation(Level.FATAL, mc, mf, this.getClass(), "@" + Version.class.getSimpleName() + " can only be used on a Long/long field.")); } }    }
@Test public void testCheck() { new AssertedFailure(ConstraintViolationException.class) { public void thisMustFail() throws Throwable { morphia.map(Fail1.class); } }; new AssertedFailure(ConstraintViolationException.class) { public void thisMustFail() throws Throwable { morphia.map(Fail2.class); } }; morphia.map(OK1.class); morphia.map(OK2.class); }