JPAContainer + FieldFactory ManyToOne NotNull problem

Hello,

my problem is that I have couple of JPA entities with ManyToOne relations. I’m using JPAContainer’s FieldFactory to create edit fields in a table.
It works correctly except one thing: For NotNull attribute I would expect that Select component should not contain empty value, because for this attribute NULL value is not allowed. But it shows besides other values also empty value.

How to force FieldFactory to generate Select component without empty values for NotNull attributes?

I didn’t find solution yet, but I maybe found direction:

I’ve overriden com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory with following method:

    @Override
    protected Field createManyToOneField(EntityContainer containerForProperty, Object itemId, Object propertyId,
            Component uiContext) {
        Field field = super.createManyToOneField(containerForProperty, itemId, propertyId, uiContext);
[b]
        if (field instanceof NativeSelect) {
            NativeSelect select = (NativeSelect) field;
            select.setNullSelectionAllowed(false);
        }
[/b]
        return field;
    }

which is working fine, but the problem is that I want to do this only for attributes which are annotated by
nullable = false
.
Do you know how to find out what annotations are set for specific property?

Finally I solved it. See following code. I’ve overriden createField method from com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory class.
In method
isValid
you can see how I check if property has nullable annotation set to false.


public Field createField(final Container container, final Object itemId, final Object propertyId,
		final Component uiContext) {
	Field field = super.createField(container, itemId, propertyId, uiContext);
	
	// ... Some other code ...
	
	// Validator, currently used just to check null values on attributes
	// which have nullable=false
	if (container != null && (container instanceof JPAContainer<?>)) {
		field.setInvalidAllowed(false);
		field.addValidator(new Validator() {
			@Override
			public void validate(Object value) throws InvalidValueException {
				if (!isValid(value)) {
					if (value == null) {
						throw new InvalidValueException("This field is mandatory, value must be set");
					} else {
						throw new InvalidValueException("Value " + value + " is not allowed for this field");
					}
				}
			}

			@Override
			public boolean isValid(Object value) {
				// Don't allow null for field with nullable=false
				if (value == null) {
					JPAContainer<?> jpaCont = (JPAContainer<?>) container;
					Class<?> entityClass = jpaCont.getEntityClass();
					try {
						java.lang.reflect.Field field2 = entityClass.getField((String) propertyId);
						Column annotation = field2.getAnnotation(Column.class);
						if (annotation != null) {
							if (annotation.nullable() == false) {
								return false;
							}
						}
						JoinColumn annotation2 = field2.getAnnotation(JoinColumn.class);
						if (annotation2 != null) {
							if (annotation2.nullable() == false) {
								return false;
							}
						}
					} catch (SecurityException e) {
					} catch (NoSuchFieldException e) {
					}
				}
				return true;
			}
		});
	}	
	
	// ... Some other code ...

	return field;
}