Hilla AutoCrud's submmit button won't persist data on database

Hello, I’ve just created a sample project with Hilla and it’s AutoCrud component won’t persist data into database. Here are the code involved:

escolas.tsx

import { ViewConfig } from '@vaadin/hilla-file-router/types.js';
import { AutoCrud } from '@vaadin/hilla-react-crud';
import { EscolaEndpoint } from 'Frontend/generated/endpoints';
import EscolaModel from 'Frontend/generated/com/example/data/EscolaModel';

export const config: ViewConfig = {
	menu: { order: 2, icon: 'line-awesome/svg/school-solid.svg' },
	title: 'Escolas',
	rolesAllowed: ['ADMIN'],
};

export default function EscolasView() {
	return (

		<div className="flex flex-col v-full items-center justify-center p-l text-center box-border">

			<AutoCrud service={EscolaEndpoint} model={EscolaModel} />

		</div>

	);
}

EscolaEndpoint.java

package com.example.services;

import com.vaadin.hilla.BrowserCallable;
import com.vaadin.hilla.Endpoint;
import com.vaadin.hilla.crud.CrudRepositoryService;

import com.example.data.Escola;
import com.example.data.EscolaRepository;
import jakarta.annotation.security.RolesAllowed;

@Endpoint
@BrowserCallable
@RolesAllowed("ADMIN")
public class EscolaEndpoint  extends CrudRepositoryService<Escola, Long, EscolaRepository>  {

}

EscolaRepository.java

package com.example.data;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface EscolaRepository extends JpaRepository<Escola, Long>, JpaSpecificationExecutor<Escola> {

}

Escola.java

package com.example.data;

import jakarta.persistence.Entity;

@Entity
public class Escola extends AbstractEntity {

	private String nome;
	private String logradouro;
	private String bairro;
	private String cidade;
	private String uf;
...

Hi Rafael,

Do you see any kind of errors messages or warnings in the backend logs or in the browser dev tools console?

What kind of database do you use? Is it a h2 in-memory-database?

I think your Escola entity could need an id field with @Id and @GeneratedValue annotations from Spring Data.

Thank you for your answer, I didn’t mention that @id and @version came from AbstractEntity, no errors or warnings, i’m using postgresql database.

The problem is only when I try to add a new entry on database, when I click on an item I can edit it and changes get persisted on database.

Try turning on hibernate logging and see if you can spot anything there

Nothing happens when I click the button, I can only edit existing data.

Hibernate: select e1_0.id,e1_0.bairro,e1_0.cidade,e1_0.logradouro,e1_0.nome,e1_0.uf,e1_0.version from escola e1_0 where e1_0.id=?
Hibernate: update escola set bairro=?,cidade=?,logradouro=?,nome=?,uf=?,version=? where id=? and version=?
Hibernate: select e1_0.id,e1_0.bairro,e1_0.cidade,e1_0.logradouro,e1_0.nome,e1_0.uf,e1_0.version from escola e1_0 order by e1_0.nome offset ? rows fetch first ? rows only
Hibernate: select count(e1_0.id) from escola e1_0

Could you share your code in a public GitHub repo or could you create an example project to reproduce the issue and share this example project as a public GitHub repo?

Sure! Here it is, thank you for help.

I had a look at your code and started your project on my computer and I can reproduce your problem. It is very strange…

Have you been able to solve your problem? I’m curious to know what caused this strange problem.

Hi, i’m courious too, as i’m facing the same problem. I recently started looking into Hilla and tried a simple example app. The submit button of autcrud (and autform) does nothing when clicked.

I tried it in two different environments (work and home) with H2 an Postgres.

The problem in the provided repo is that all properties on all entities are marked as not nullable (via package-info.java), which tells Hilla (the Hilla form specifically) that all properties must have a value in order to successfully submit a form. However that’s not the case for id and version as these values are generated only on the server after saving an entity. The other problem is that Hilla validates these properties 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.

Not sure what the proper way to deal with this should be. What works is marking both the id and version properties as nullable by adding @com.vaadin.hilla.Nullable to each property. Maybe there is some other mechanism for making the Hilla form ignore certain properties that are not meant to be edited.

1 Like

Thank you, that did the trick. I didn’t notice the @NonNullApi annotation and was looking somewhere else, expecting erros messages.

Hey @sissbruecker,
do you know if there is already a GitHub issue for this? I think this should be addressed to improve the developer experience.

I do not know if there is an issue.

Now there is one ;)