Spring project not saved in Mysql

Asked

Viewed 128 times

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?

  • Philip, I have beheaded him and he is not entering this instruction

  • So, find the problem. : ) Now, check the annotations and see why you’re not calling this method.

1 answer

0

If you just want to run a simple code, you can use the interface CommandLineRunner in its main class. Spring itself injects an implementation for its Userrepository class.

OBS: Every time you start the server, your database will be populated, if you want to work with data migration I suggest using Liquibase or Flyway

@SpringBootApplication
public class App {

  public static void main(String[] args) {
    SpringApplication.run(SpringBootConsoleApplication.class, args);

  }

  @Bean
  CommandLineRunner runner(UserRepository ur) {
    return args -> {
       ur.save(entity) \\ codigo que voce quiser executar após inicializacao
    };
  }


}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.