I have a visitor record in which I fill in the data (title and date), and hibernate executes when requesting "save" on the form.
Here is the log when I ask to save in bank (MySql).
12:37:27.752 DEBUG org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl: Adding composing constraint: ConstraintDescriptorImpl{annotation=javax.validation.constraints.NotNull, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={message={javax.validation.constraints.NotNull.message}, groups=[Ljava.lang.Class;@5c6bf3c2, payload=[Ljava.lang.Class;@174cdbab}, constraintType=GENERIC}.
12:37:27.862 DEBUG org.hibernate.engine.transaction.internal.TransactionImpl: begin
12:37:27.919 DEBUG org.hibernate.engine.spi.ActionQueue: Executing identity-insert immediately
12:37:27.937 DEBUG org.hibernate.SQL: insert into visita (data, titulo) values (?, ?)
12:37:27.984 DEBUG org.hibernate.id.IdentifierGeneratorHelper: Natively generated identity: 11
12:37:27.984 DEBUG org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl: HHH000387: ResultSet's statement was not registered
12:37:27.991 DEBUG org.hibernate.engine.transaction.internal.TransactionImpl: committing
12:37:27.993 DEBUG org.hibernate.event.internal.AbstractFlushingEventListener: Processing flush-time cascades
12:37:27.995 DEBUG org.hibernate.event.internal.AbstractFlushingEventListener: Dirty checking collections
12:37:27.999 DEBUG org.hibernate.event.internal.AbstractFlushingEventListener: Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
12:37:27.999 DEBUG org.hibernate.event.internal.AbstractFlushingEventListener: Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
12:37:28.002 DEBUG org.hibernate.internal.util.EntityPrinter: Listing entities:
12:37:28.002 DEBUG org.hibernate.internal.util.EntityPrinter: model.Visita{data=2019-03-12, titulo=aaa, id=11}
From what I understand the data is not passing to the insert command, but it appears in the "Model" at the end of the execution. But I do not know how to solve this ... I already researched for answers and I did not find anything that solved.
EDIT:
The error is triggered when Spring executes this Service:
@Service
public class CadastroVisitaService {
@Autowired
private Visitas visitas;
@Transactional
public void salvar(Visita visita) {
try {
visitas.save(visita);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Important codes for understanding:
Visitor Repository
@Repository
public interface Visitas extends JpaRepository<Visita,Long> {
}
Visitor Controller
@Controller
public class VisitasController {
@Autowired
private CadastroVisitaService cadastroVisitaService;
@RequestMapping("/visitas/novo")
public ModelAndView novo(Visita visita) {
ModelAndView mv = new ModelAndView("visita/CadastroVisita");
return mv;
}
@RequestMapping(value = "/visitas/novo", method = RequestMethod.POST)
public ModelAndView cadastrar(@Valid Visita visita, BindingResult result, Model model, RedirectAttributes attributes) {
if (result.hasErrors()) {
return novo(visita);
}
try {
cadastroVisitaService.salvar(visita);
} catch (Exception ex) {
ex.printStackTrace();
}
attributes.addFlashAttribute("mensagem", "Visita salva com sucesso!");
return new ModelAndView("redirect:/visitas/novo");
}
}
Entity Visit
@Entity
@Table(name = "visita")
public class Visita {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Titulo é obrigatório")
private String titulo;
@NotNull(message = "Data é obrigatória")
@Data
private LocalDate data;
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public LocalDate getData() {
return data;
}
public void setData(LocalDate data) {
this.data = data;
}
}
Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context>
<Resource name="jdbc/visitas" auth="Container"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
driverClass="com.mysql.jdbc.Driver"
jdbcUrl="jdbc:mysql://localhost/visitas?useSSL=false"
user="root"
password="root"
initialPoolSize="5"
minPoolSize="5"
maxPoolSize="5"
/>
</Context>
I'm using Thymeleaf in the frontend, with pure html.