ManyToOne-Relation in AutoForm

Hi everyone :wave:,
this is related to ManyToOne-Relation in AutoGrid. I have two simple Entities called Order and Customer . An Order has exactly one Customer (and a Customer can have 0 or more Orders ). The entities are defined like this:

@Entity
@Table(name = "orders")
public class Order {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    Long id;

    @NotNull
    private String number;

    @NotNull
    private LocalDate created;

    @NotNull
    @ManyToOne(targetEntity = Customer.class, optional = false)
    private Customer customer;

   // Getters and Setters omitted

}
@Entity
@Table(name = "customers")
public class Customer {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    Long id;

    @NotBlank
    String name;

    @NotBlank
    @Email
    String mail;

   // Getters and Setters omitted   
}

I would like to use AutoForm to add and edit an order. The corresponding OrderService is implemented like this:

@BrowserCallable
@AnonymousAllowed
public class OrderService extends CrudRepositoryService<Order, Long, OrderRepository> {}

If I use AutoForm like this:

<AutoForm model={OrderModel} service={OrderService} item={order} />

I see the following result:

The field for Customer is rendered as a TextArea. That really surprised me and I’m not sure if that’s what is intended?

It is easy to change AutoForm using as custom renderer for Customer like this:

  fieldOptions={{
    customer: {
      renderer: ({ field }) => <ComboBox {...field} items={customers} itemLabelPath='name' />,
    },
  }}

This is fine and works as expected. However, I would like to know if there is another or easier way to support the ManyToOne-Relation? The default behavior of AutoForm seems strange to me.

To my knowledge, there isn’t a more straightforward way.
But there already is such a feature request for the AutoForm component.

You can leave a like to top comment or add any details you wish for.

2 Likes

Thank you @kriss.1 for your response and thank you for referencing the GitHub issue. I was not aware of this. I left an upvote on the issue.

I just wanted to leave a notice, that I wrote a little blog post about 1:n relationships in Hilla with AutoGrid and AutoForm: https://www.rene-wilby.de/en/blog/one-to-many-relations-with-hilla-autogrid-and-autoform/. The blog post covers some of the aspect discussed here.

2 Likes