Home > front end >  Vaadin binder writing to an instance of an inherited abstract class
Vaadin binder writing to an instance of an inherited abstract class

Time:12-10

In my Vaadin app I'm trying to build an user administration view. It should be made of a CRUD component capable of maintaining multiple user roles. Every role has it's own class.

This is my class hierarchy. It all starts with a common ancestor:

public abstract class AbstractUser {

    private String name;
    private Color favColor;

    ... other common fields and methods ...

}

Then it's children:

public class User extends AbstractUser {

    ... additional methods ...

}
public class Admin extends User {

    ... additional methods ...

}
public class SuperAdmin extends Admin {

    ... additional methods ...

}

Stumbling block: You can't use com.vaadin.flow.data.binder.Binder, which I'm used to, with an abstract class to administer all of the user types. When I try to click on the New Item button in the crud component, the binder tries to create a new instance of AbstractUser, which of course fails.

So what would be the best way to handle this?

All classes have identical fields, all these fields are set by the editor. I don't need to change user roles in this view, all I want is that the binder wouldn't fail when creating a new user. There is no need for a new item editor capable of creating users of another type than the basic User.

I have a few ideas on how to do this, but I want the cleanest solution.

CodePudding user response:

The Crud component cannot work with abstract classes. I have solved this problem by not using the AbstractUser and creating a crud with generic type User, so it can also be filled with Admin and SuperAdmin.

  • Related