One table is not being created - JPA/Hibernate with Mysql

Asked

Viewed 155 times

2

I’m having a problem running my test class:

import javax.persistence.EntityManager;

import br.com.teste.jpa.c_mapeamento.model.Conta;

public class PopulaConta {

    public static void main(String[] args) {

        EntityManager manager = JPAUtil.getEntityManager();
        manager.getTransaction().begin();

        Conta conta1 = new Conta();
        Conta conta2 = new Conta();
        Conta conta3 = new Conta();

        conta1.setBanco("001 - BANCO DO BRASIL");
        conta1.setNumero("16987-8");
        conta1.setAgencia("6543");
        conta1.setTitular("Maria dos Santos");

        conta2.setBanco("237 - BANCO BRADESCO");
        conta2.setNumero("86759-1");
        conta2.setAgencia("1745");
        conta2.setTitular("Paulo Roberto Souza");

        conta3.setBanco("341 - BANCO ITAU UNIBANCO");
        conta3.setNumero("46346-3");
        conta3.setAgencia("4606");
        conta3.setTitular("Antonio Duraes");

        // persistindo as contas
        manager.persist(conta1);
        manager.persist(conta2);
        manager.persist(conta3);

        manager.getTransaction().commit();
        manager.close();

    }
}

The above class has no problems in the code, the problem is that it does not create the client table automatically and says that this table does not exist.

Log:

ago 30, 2018 9:34:04 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
ago 30, 2018 9:34:04 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.9.Final}
ago 30, 2018 9:34:04 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
ago 30, 2018 9:34:04 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
ago 30, 2018 9:34:05 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
ago 30, 2018 9:34:05 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
ago 30, 2018 9:34:05 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
ago 30, 2018 9:34:05 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost/teste?useSSL=false&serverTimezone=UTC]
ago 30, 2018 9:34:05 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {autocommit=true, release_mode=auto, user=root}
ago 30, 2018 9:34:05 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
ago 30, 2018 9:34:05 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
ago 30, 2018 9:34:05 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by javassist.util.proxy.SecurityActions (file:/G:/workspace_alura/JPA%20Persista%20Objetos/WebContent/WEB-INF/lib/javassist-3.17.1-GA.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of javassist.util.proxy.SecurityActions
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: teste.categoria
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [nome, id]
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: db.cliente
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [nome, id]
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: teste.conta
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [numero, banco, id, agencia, titular]
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
ago 30, 2018 9:34:05 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: teste.movimentacao
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [tipo, data, conta_id, valor, id, descricao]
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: [fk257eb9231c7c520e]
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [fk257eb9231c7c520e, primary]
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: teste.movimentacao_categoria
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [categoria_id, movimentacao_id]
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: [fkfc77e4f7724131a6, fkfc77e4f72b7c518e]
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [fkfc77e4f7724131a6, fkfc77e4f72b7c518e]
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000388: Unsuccessful: alter table Cliente add column endereco varchar(255)
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: Table 'teste.cliente' doesn't exist
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000388: Unsuccessful: alter table Cliente add column profissao varchar(255)
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: Table 'teste.cliente' doesn't exist
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000388: Unsuccessful: alter table Cliente add column conta_id integer unique
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: Table 'teste.cliente' doesn't exist
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000388: Unsuccessful: alter table Cliente add index FK96841DDAA1A8D395 (conta_id), add constraint FK96841DDAA1A8D395 foreign key (conta_id) references Conta (id)
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: Table 'teste.cliente' doesn't exist
ago 30, 2018 9:34:06 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete

Entidade Cliente

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Cliente {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String nome;
    private String profissao;
    private String endereco;

    @OneToOne
    @JoinColumn(unique = true)
    private Conta conta;

    public Cliente() {
    }

    public Cliente(String nome) {
        this.nome = nome;
    }

    // getters e setters    
}

This occurs only when using Mysql. Using Postgresql works smoothly.

Here is my persintence.xml file:

<persistence-unit name="contas-mysql">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>br.com.teste.jpa.c_mapeamento.model.Conta</class>
    <class>br.com.teste.jpa.c_mapeamento.model.Movimentacao</class>
    <class>br.com.teste.jpa.c_mapeamento.model.Categoria</class>
    <class>br.com.teste.jpa.c_mapeamento.model.Cliente</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/teste?useSSL=false&amp;serverTimezone=UTC" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="" />

        <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-unit name="contas-postgre">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>br.com.teste.jpa.c_mapeamento.model.Conta</class>
    <class>br.com.teste.jpa.c_mapeamento.model.Movimentacao</class>
    <class>br.com.teste.jpa.c_mapeamento.model.Categoria</class>
    <class>br.com.teste.jpa.c_mapeamento.model.Cliente</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/alura" />
        <property name="javax.persistence.jdbc.user" value="postgres" />
        <property name="javax.persistence.jdbc.password" value="ROOT" />

        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
    </properties>
</persistence-unit>
  • 2

    "Table found: test.category", "Table found: db client.", "Table found: test.", "Table found: test.movement", "Table found: teste.movimentao_categoria", "Table test customer.' doesn’t exist" - Note that all tables are in the package teste, except that of customers who are in db, but something tries to modify it as if it were in teste. Are you sure you don’t have two classes called Cliente? Or else, you’ve had it before and the project hasn’t been properly cleaned?

  • I changed some things like packages to make them more organized. The problem only happens if I use mysql. With postgre this error does not happen. I don’t understand why. Did I miss or miss something in persinstence.xml?

  • The log is with the changes too.

  • I found the problem! This db.client that is in the log is another database that I have and that has the client table in it. I deleted this database db and created the table client in the database test. I still don’t know why, since they are in different databases. :(

No answers

Browser other questions tagged

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