(hibernate) Tables are created, but data is not inserted

Asked

Viewed 46 times

0

I am trying to create and insert data into a table through Hibernate, but when I run the code no data is inserted.

I have a medical class that inherits from the Employee class.

Staff Class:

  import java.io.Serializable;
  import javax.persistence.*;

  @Entity
  @Inheritance(strategy = InheritanceType.JOINED)
  @Table(name="funcionario")
  public class Funcionario implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
    private String matricula;
    private String nome;
    private String senha;
    private String cpf;
    private String cargo;
    private String horaInicioTrab;
    private String horaFinalTrab;


    public String getMatricula() {
        return matricula;
    }

    public String getSenha() {
        return senha;
    }

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

    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getCpf() {
        return cpf;
    }
    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public String getCargo() {
        return cargo;
    }
    public void setCargo(String cargo) {
        this.cargo= cargo;
    }
    public String getHoraInicioTrab() {
        return horaInicioTrab;
    }
    public void setHoraInicioTrab(String horaInicioTrab) {
        this.horaInicioTrab = horaInicioTrab;
    }
    public String getHoraFinalTrab() {
        return this.horaFinalTrab;
    }
    public void setHoraFinalTrab(String horaFinalTrab) {
        this.horaFinalTrab = horaFinalTrab;
    }
}

Classe Medico:

import javax.persistence.*;

@Entity
@Table(name = "medico")
public class Medico extends Funcionario {

    private String crm;

    public String getCrm() {
        return crm;
    }

   public void setCrm(String crm) {
        this.crm = crm;
    }
}

Medicodao class:

import JavaBeans.Medico;
import conexao.ConnectionFactory;
import javax.persistence.EntityManager;

public class MedicoDao {
    public Medico adiciona(Medico medico){
        EntityManager em = new ConnectionFactory().getConnection();
        try {
            em.getTransaction().begin();
            em.persist(medico);
            em.getTransaction().commit();

        }catch (Exception e){

            em.getTransaction().rollback();
        } finally {
            em.close();
        }
          return medico;
    }
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence         http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<!-- Unidade de persistencia -->
<persistence-unit name="meuPU">
    <!-- Implementação do JPA -->
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <!-- Lista de entidades -->
    <class>JavaBeans.Funcionario</class><!--Class Funcionario-->
    <class>JavaBeans.Medico</class><!--Class Medico-->
    <class>JavaBeans.Endereco</class><!--Class Endereco-->
    <properties>
        <!-- Propriedades JDBC -->
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/dbHospital"/><!--banco usado-->
        <property name="javax.persistence.jdbc.user" value="root"/><!--usuario-->
        <property name="javax.persistence.jdbc.password" value=""/><!--senha-->
        <!-- Configurações específicas do Hibernate -->
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
    </properties>
</persistence-unit>
</persistence>
  • You’ve already put a breakpoint or log into this silent Exception treatise where you just do the rollback inside Medicodao.()?

  • Above the attributes of your model/entity, make it clear that they will be columns: @Column

No answers

Browser other questions tagged

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