0
I created a project with Spring boot and cannot save data in Mysql. No error message is displayed. Spring creates tables, but does not insert data.
User class
package com.projeto.principal.entity;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String nome;
private String email;
@ManyToMany
private Set<Role> roles;
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public User() {
}
public User(String nome, String email) {
super();
this.nome = nome;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Userrepository interface
package repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.projeto.principal.entity.User;
public interface UserRepository extends JpaRepository<User, Long>{
}
Data Initialization Class
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import com.projeto.principal.entity.User;
import repository.UserRepository;
@Component
public class DataInicializr implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
UserRepository userRepository;
@Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {
//Sempre vai rodar no inicio da aplicacao
User user = new User();
user.setEmail("[email protected]");
user.setNome("Maria");
userRepository.save(user);
}
}
Came to debug p/ check if this statement is actually being executed?
– Filipe L. Constante
Philip, I have beheaded him and he is not entering this instruction
– user3337096
So, find the problem. : ) Now, check the annotations and see why you’re not calling this method.
– Filipe L. Constante