must contain default constructor when using NestedMethodProperty (JPA)

Hi again!

I am using NestedMethodProperty to add properties to my company bean. I got very strange terror message, because I do have a default constructor in my Company class among the other constructors.

Caused by: java.lang.IllegalArgumentException: Class ‘class com.xxxxx.adminTool.domain.Company’ must contain default constructor
at com.vaadin.addon.beanvalidation.BeanValidationValidator.(BeanValidationValidator.java:65)
at com.vaadin.addon.beanvalidation.BeanValidationValidator.addValidator(BeanValidationValidator.java:87)
at com.vaadin.addon.beanvalidation.BeanValidationForm.addField(BeanValidationForm.java:63)
at com.vaadin.ui.Form.setItemDataSource(Form.java:771)
at com.vaadin.addon.beanvalidation.BeanValidationForm.setItemDataSource(BeanValidationForm.java:106)

newCompanyItem.addItemProperty("address1", new NestedMethodProperty(newCompany, "address.address1"));
editorForm.setItemDataSource(companyItem, Arrays.asList("companyName",
                "businessId", "vatNumber", "address1"));
public class Company implements java.io.Serializable {

	
	private static final long serialVersionUID = -6779355918409596943L;
	@Id
        @GeneratedValue(strategy = GenerationType.AUTO)
	private Short companyId;
	@OneToOne(cascade=CascadeType.PERSIST)
	private Address address;
	private String companyName;
	private String businessId;
	private String vatNumber;
	
	private String phoneNumber;
	@Temporal(TemporalType.DATE)
	private Date lastUpdate;
	
	
	public Company() {
         super();
	}

I haven’t got knowledge enough to override addField-method in BeanValidationForm. Could someone help me a little bit? So the beanClass is instance of Company and inside the company there is an Address-object and I am trying to use those nested properties like address.address1 etc? I think that this is the problem, beanvalidator just doesn’t work with JPA and beans or is there any another solution?

public void addField(Object propertyId, Field field) {
        Item item = getItemDataSource();
        field.setPropertyDataSource(item.getItemProperty(propertyId));
        Class<? extends T> beanClass = this.beanClass;
        if (item instanceof BeanItem) {
            beanClass = (Class<? extends T>) ((BeanItem<T>) item).getBean()
                    .getClass();
        }
        BeanValidationValidator validator = BeanValidationValidator
                .addValidator(field, propertyId, beanClass);
        if (locale != null) {
            validator.setLocale(locale);
        }
        super.addField(propertyId, field);
    }

Sami

Hi, this post it’s a little bit old but I’m facing the exact same problem and seems that there is no information or documentation about it. Would you mind sharing how you solved the problem?

Hi, I have the same problem and I think there is a problem in the code of BeanValidationValidator.

If you look at the
source code
, there is a problem of try/catch in the constructor :

try {
            method = new MethodProperty(beanClass.newInstance(), propertyName);
        } catch (Exception e) {
            throw new IllegalArgumentException("Class '" + beanClass
                    + "' must contain default constructor");
        }

It’s not a very good idea to catch Exception… If another exception that extends Exception occurs, the message will always be the same. Or at least, it should be interesting to put the e parameter as the second parameter of IllegalArgumentException in order to preserve the stack trace.

What do you think ?