Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling of @Id and @Version fields in AutoCrud could be improved #2809

Open
rbrki07 opened this issue Oct 9, 2024 · 3 comments · May be fixed by #2833
Open

Handling of @Id and @Version fields in AutoCrud could be improved #2809

rbrki07 opened this issue Oct 9, 2024 · 3 comments · May be fixed by #2833
Assignees
Labels
bug Something isn't working hilla Issues related to Hilla

Comments

@rbrki07
Copy link
Contributor

rbrki07 commented Oct 9, 2024

Describe the bug

When you have a JPA entity with a field annotated with @Id or @Version and you use this entity in the AutoCrud component, you likely won't be able to submit the form of the AutoCrud component. You can fill the form and press submit, but nothing will happen, and you won't see any errors in the frontend or backend.

Hilla validates these fields already on the client, but the error is never shown because AutoCrud / AutoForm hide fields for id and version automatically. So there are client-side validation errors, but they are not visible. The values for id and version fields are generated only on the server after saving an entity.

A workaround is to add the @com.vaadin.hilla.Nullable annotation to the id and/or version field of the JPA entity, but this feels strange. From a developer perspective, I want this fields to be not empty nor null, and I know, that JPA will take care of this.

Expected-behavior

Using the annotations @Id and @Version is quite common. As a developer, I want to use entities with these annotations in the AutoCrud component with a good developer experience. This could mean:

  • Getting an error/warning when the validation of the hidden id and/or version field in the AutoCrud components fails. This could be a console.error or a notification or a popup/popover.
  • AutoCrud could skip validation for fields that are hidden per default.

Reproduction

  • Create a new Hilla project using start.vaadin.com or Vaadin CLI
  • Add an entity with id and/or version field and @Id and/or @Version annotations
  • Create a JpaRepository and CrudRepositoryService for that entity
  • Create a view, add the AutoCrud component to that view and configure the component to use the generated model and service of that entity.
  • Try to add a new item with the form component of the AutoCrud component

System Info

Vaadin 24.5.0.beta6

@rbrki07 rbrki07 added bug Something isn't working hilla Issues related to Hilla labels Oct 9, 2024
@rbrki07
Copy link
Contributor Author

rbrki07 commented Oct 9, 2024

@platosha
Copy link
Contributor

See #2217 specifically about the errors from hidden fields. Besides that, we could assume @Nullable for such @Id and @Version fields, allowing the value to be undefined on the client side.

@rbrki07
Copy link
Contributor Author

rbrki07 commented Oct 13, 2024

I think I need to add some more context. I detected a typo in my example code

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

For some reason I don't understand, I used long instead of Long which results in the behaviour described above.

Using private long id means that I was not able to submit a AutoCrud or AutoForm component, because Hilla's client-side validation prevents the form from being submitted to the backend.

Using private Long id works just fine, which means that I was able to submit a AutoCrud or AutoForm component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hilla Issues related to Hilla
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants