Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

JPAContainer's FieldFactory and Bean Validation

Martin Hjalmarsson
10 years ago Mar 20, 2012 8:21am

Hi,

I recently started using Vaadin and I guess I love it. When I eventually get a hang of it, I think it will save A LOT of my development time.

That said, I have a question / suggestion regarding the Vaadin JPAContainer and Vaadin Bean Validation addons.

Both plugins seems great, however in some instances they do not work very well together, either that or I am missing something.

Given this persistence-model:

@Entity
public class Person {
    
    @Id
    @GeneratedValue
    private long id;
    
    @Size(min=2,max=32)
    private String name;
    
    @Size(min=2,max=32)
    private String phone;

    @Size(min=2,max=32)
    private String email;
    
    @NotNull
    @OneToOne
    private Address address;

    ...
}

@Entity
public class Address {
    
    @Id
    @GeneratedValue
    private long id;
    
    @NotNull
    @Size(min=2,max=64)
    private String row1;
    
    @Size(min=2,max=64)
    private String row2;
    
    @NotNull
    @Size(min=5,max=6)
    private String zipcode;
    
    @NotNull
    @Size(min=2,max=64)
    private String city;

    ...
}

The JPAContainers FieldFactory will generate a OneToOneForm for input of the address, this form will, understandably, not have validation. Even if I generate the form like this:

final JPAContainer<Person> container = JPAContainerFactory.makeBatchable(Person.class, PERSISTENCE_UNIT);
        container.setAutoCommit(false);
        
        final Form form = new BeanValidationForm<Person>(Person.class);
        form.setWriteThrough(false);
        form.setImmediate(true);
        form.setFormFieldFactory(new com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory());
        form.setItemDataSource(container.getItem(container.addItem()));
        form.setVisibleItemProperties(new String[] {"name","phone","email","address"});
        form.getFooter().addComponent(new Button("Save", new Button.ClickListener() {
            
            public void buttonClick(ClickEvent event) {
                form.commit();
                container.commit();
            }
            
        }));
        mainWindow.addComponent(form);

On validation of the form and there's a problem with the address, it just says that, that there's something wrong with the address, not what's wrong. (See screenshot)

I was looking into the FieldFactory to perhaps override the OneToOneForm-generation, unfortunately the FieldFactory requires an instance of OneToOneForm so I cannot give it a BeanValidationForm<Address>(Address.class).

Am I doing something wrong?
If not, couldn't the OneToOneForm be an interface or does FieldFactory.createOneToOneField(EntityContainer, Object, Object, Component) really have to return a OneToOneForm? Looking at the source, the OneToOneForm is only referenced in that method so why not let it return a Field as the name of the method suggests?

Full test source is attached, with screenshot of validation error in form.

P.S. Why do I not seem to get a caption for my OneToOneForm? As in the screenshot on the addon-page.

Last updated on Mar, 20th 2012
Martin Hjalmarsson
10 years ago Mar 20, 2012 10:46am