Problems with Springboot - o.s.boot.Springapplication : Application run failed

Asked

Viewed 638 times

-1

Error Starting Applicationcontext. To display the conditions report re-run your application with 'debug' enabled. 2021-05-13 08:05:53.688 ERROR 14840 --- [ restartedMain] o.s.boot.Springapplication : Application run failed

org.springframework.Beans.factory.Beancreationexception: Error Creating bean with name 'entityManagerFactory' defined in class Repath source [org/springframework/boot/autoconfigure/Orm/jpa/Hibernatejpaconfiguration.class]: Invocation of init method failed; nested exception is java.lang.ClassCastException: class org.hibernate.mapping.SingleTableSubclass cannot be cast to class org.hibernate.mapping.RootClass (org.hibernate.mapping.SingleTableSubclass and org.hibernate.mapping.RootClass are in unnamed module of loader 'app') at org.springframework.Beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(Abstractautowirecapablebeanfactory.java:1786) ~[spring-Beans-5.3.6.jar:5.3.6] at org.springframework.Beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(Abstractautowirecapablebeanfactory.java:602) ~[spring-Beans-5.3.6.jar:5.3.6] at org.springframework.Beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(Abstractautowirecapablenfactory.java:524) ~[spring-Beans-5.3.6.jar:5.3.6] at org.springframework.Beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(Abstractbeanfactory.java:335) ~[spring-Beans-5.3.6.jar:5.3.6] at org.springframework.Beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(Defaultsingletonbeanregistry.java:234) ~[spring-Beans-5.3.6.jar:5.3.6] at org.springframework.Beans.factory.support.AbstractBeanFactory.doGetBean(Abstractbeanfactory.java:333) ~[spring-Beans-5.3.6.jar:5.3.6] at org.springframework.Beans.factory.support.AbstractBeanFactory.getBean(Abstractbeanfactory.java:208) ~[spring-Beans-5.3.6.jar:5.3.6] at org.springframework.context.support.Abstractapplicationcontext.getBean(Abstractapplicationcontext.java:1154) ~[spring-context-5.3.6.jar:5.3.6] at org.springframework.context.support.Abstractapplicationcontext.finishBeanFactoryInitialization(Abstractapplicationcontext.java:908)

Process finished with Exit code 0

My kind of connection:

import javax.sql.Datasource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.jdbc.datasource.Drivermanagerdatasource; import org.springframework.Orm.jpa.Jpavendoradapter; import org.springframework.Orm.jpa.vendor.Database; import org.springframework.Orm.jpa.vendor.Hibernatejpavendoradapter;

@Configuration @Profile({"dev"}) public class Conexaomysql { public Conexaomysql() { }

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/keyfalls?useTimezone=true&serverTimezone=UTC");
    dataSource.setUsername("root");
    dataSource.setPassword("");
    return dataSource;
}

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
    adapter.setDatabase(Database.MYSQL);
    adapter.setShowSql(true);
    adapter.setGenerateDdl(true);
    adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
    adapter.setPrepareConnection(true);
    return adapter;
}

}

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Generatedvalue; import javax.persistence.Generationtype; import javax.persistence.Id; import javax.persistence.Table;

import java.io.Serializable;

@Entity @Table(name="User") public class User Implements Serializable { /** * */ private Static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
@Column(name="username")
String username;
@Column(name="senha")
String senha;
@Column(name="nome")
String nome;
@Column(name="cpf")
String CPF;
@Column(name="cep")
String CEP;
@Column(name="endereco")
String endereco;
@Column(name="telefone")
String telefone;
@Column(name="endereco_email")
String endereco_email;
@Column(name="sexo")
char sexo;

// funções

public Usuario(int id, String username, String senha, String nome, String cPF, String cEP, String endereco,
               String telefone, String endereco_email, char sexo) {
    super();
    this.id = id;
    this.username = username;
    this.senha = senha;
    this.nome = nome;
    CPF = cPF;
    CEP = cEP;
    this.endereco = endereco;
    this.telefone = telefone;
    this.endereco_email = endereco_email;
    this.sexo = sexo;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getSenha() {
    return senha;
}

public void setSenha(String senha) {
    this.senha = senha;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getCPF() {
    return CPF;
}

public void setCPF(String cPF) {
    CPF = cPF;
}

public String getCEP() {
    return CEP;
}

public void setCEP(String cEP) {
    CEP = cEP;
}

public String getEndereço() {
    return endereco;
}

public void setEndereço(String endereco) {
    this.endereco = endereco;
}

public String getTelefone() {
    return telefone;
}

public void setTelefone(String telefone) {
    this.telefone = telefone;
}

public String getEndereco_email() {
    return endereco_email;
}

public void setEndereco_email(String endereco_email) {
    this.endereco_email = endereco_email;
}

public char getSexo() {
    return sexo;
}

public void setSexo(char sexo) {
    this.sexo = sexo;
}

// Função que permite ao usuario se cadastrar
public void cadastrar() {

}

//

// Função que permite ao usuario entar no sistema
public void logar() {

}

}

import java.sql.Date;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table;

@Entity @Table(name="Candidate") public class Applicant extends Usuario {

/**
 *
 */
private static final long serialVersionUID = 1L;


int inscricao;

Locais_Prova locais_prova;
Salas[] salas;
Cargo cargo;
Prova[] prova;

@Column(name="data_de_inscricao")
Date data_de_inscricao;
@Column(name="data_nascimento")
Date data_nascimento;
@Column(name="renda")
double renda;
@Column(name="insento")
boolean insento;
@Column(name="nota")
double nota;

//funções

public Candidato(int id, String username, String senha, String nome, String cPF, String cEP, String endereço,
                 String telefone, String endereco_email, char sexo, Locais_Prova locais_prova, Salas[] salas, Cargo cargo,
                 Prova[] prova, int inscricao, Date data_de_inscricao, Date data_nascimento, double renda, boolean insento,
                 double nota) {
    super(id, username, senha, nome, cPF, cEP, endereço, telefone, endereco_email, sexo);
    this.locais_prova = locais_prova;
    this.salas = salas;
    this.cargo = cargo;
    this.prova = prova;
    this.inscricao = inscricao;
    this.data_de_inscricao = data_de_inscricao;
    this.data_nascimento = data_nascimento;
    this.renda = renda;
    this.insento = insento;
    this.nota = nota;
}

public Locais_Prova getLocais_prova() {
    return locais_prova;
}

public void setLocais_prova(Locais_Prova locais_prova) {
    this.locais_prova = locais_prova;
}

public Salas[] getSalas() {
    return salas;
}

public void setSalas(Salas[] salas) {
    this.salas = salas;
}

public Cargo getCargo() {
    return cargo;
}

public void setCargo(Cargo cargo) {
    this.cargo = cargo;
}

public Prova[] getProva() {
    return prova;
}

public void setProva(Prova[] prova) {
    this.prova = prova;
}

public int getInscricao() {
    return inscricao;
}

public void setInscricao(int inscricao) {
    this.inscricao = inscricao;
}

public Date getData_de_inscricao() {
    return data_de_inscricao;
}

public void setData_de_inscricao(Date data_de_inscricao) {
    this.data_de_inscricao = data_de_inscricao;
}

public Date getData_nascimento() {
    return data_nascimento;
}

public void setData_nascimento(Date data_nascimento) {
    this.data_nascimento = data_nascimento;
}

public double getRenda() {
    return renda;
}

public void setRenda(double renda) {
    this.renda = renda;
}

public boolean isInsento() {
    return insento;
}

public void setInsento(boolean insento) {
    this.insento = insento;
}

public double getNota() {
    return nota;
}

public void setNota(double nota) {
    this.nota = nota;
}

//Function that allows the user to enter the competition public void inscribe(int numero_inscription) {

    this.inscricao = numero_inscricao;

}

//Função que permite ao usuario obter o boleto de pagamento
public void obter_boleto() {

}

//Função que permite ao usuario pedir a insenção de custo
public void pedir_insencao() {

}

//

//Função que permite ao candidato ver sua quantidade de erros e acertos
public void erros_e_acertos() {

}

}

import javax.persistence.Entity; import javax.persistence.Table;

@Entity @Table(name="Working") public class Funcio extends Usuario {

/**
 *
 */
private static final long serialVersionUID = 1L;


int matricula;

public Funcionario(int id, String username, String senha, String nome, String cPF, String cEP, String endereço,
                   String telefone, String endereco_email, char sexo, int matricula) {
    super(id, username, senha, nome, cPF, cEP, endereço, telefone, endereco_email, sexo);
    this.matricula = matricula;
}

public int getMatricula() {
    return matricula;
}

public void setMatricula(int matricula) {
    this.matricula = matricula;
}

}

I took @Id from the classes they were inheriting

1 answer

1


Usually, when errors occur like this, try to check the caused by described in the log.

We see the following in your mistake: Caused by: java.lang.Classcastexception: class org.hibernate.Mapping.Singletablesubclass cannot be cast to class

This error happens due to inheritance implementations problems.

If you can, post the legacy mapping cases you have.

We have 3 JPA inheritance strategies:

  • create a single table that stores all the inheritance information
  • create Table only for the concrete classes of your Inheritance
  • create table for all classes of inheritance

I don’t know if that’s the case. That’s why you need to post classes that implement inheritance here.

If you have implemented an @Inheritance(Strategy=Inheritancetype.TABLE_PER_CLASS)

And having entered @Id in the child class, will generate this error.

This is why it is important to also analyze the classes in which you are using inheritance.

Just another question I wish to add.

Your project requires you to have the datasource explicitly returned in this way?

Because if you use some spring frameworks, it may not be necessary. But of course it depends on the project as a whole to understand your needs. See if spring-date can help you too.

  • I posted another answer because it is too character to comment.

  • I think the best thing would be to edit the question.

  • I put @Inheritance(Strategy=Inheritancetype.TABLE_PER_CLASS) in the User class and started using the application.properties disabled the connection creation class

Browser other questions tagged

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