Ao rodar, estou com error. Estou usando h2
Segue meu código
package br.com.alura.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Entity
@Table(name = "cargo")
@Data
public class Cargo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String descricao;
}
Repository
package br.com.alura.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import br.com.alura.model.Cargo;
@Repository
public interface CargoRepository extends CrudRepository<Cargo,Integer> {
}
Main
package br.com.alura;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import br.com.alura.model.Cargo;
import br.com.alura.repository.CargoRepository;
@SpringBootApplication
public class SpringdataApplication implements CommandLineRunner{
private final CargoRepository cargoRepository ;
public SpringdataApplication(CargoRepository cargoRepository) {
this.cargoRepository = cargoRepository;
}
public static void main(String[] args) {
SpringApplication.run(SpringdataApplication.class, args);
}
/*Após um Start da aplicação é executado esse método.*/
@Override
public void run(String... args) throws Exception {
Cargo cargo = new Cargo();
cargo.setDescricao("Teste");
cargoRepository.save(cargo);
}
}
application.properties
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
#Ao iniciando o banco ele carrega o data.sql
spring.jpa.defer-datasource-initialization=true