Hibernate is not creating or updating BD tables

Asked

Viewed 524 times

4

I have the classes

Beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                        http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    version="1.1" bean-discovery-mode="all">

<scan>
    <exclude name="com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService"/>
</scan>

</beans>

xml validation.

<?xml version="1.0" encoding="UTF-8"?>

<validation-config xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration 
validation-configuration-1.1.xsd"
version="1.1">
<executable-validation enabled="false" />

peristence.xml

<?xml version="1.0" encoding="UTF-8"?> <persistence 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"
version="2.0">



<!-- LOCAL (DEFAULT) -->
<persistence-unit name="postgreSQL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>


    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />


        <!-- LOCAL -->

        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/rreefstore" />
        <property name="javax.persistence.jdbc.user" value="postgres" />
        <property name="javax.persistence.jdbc.password" value="dsv" />


        <!-- PRODUÇÃO -->
        <!-- <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://urlServer:5432/tfBestJob" 
            /> <property name="javax.persistence.jdbc.user" value="adminbj" /> <property 
            name="javax.persistence.jdbc.password" value="senha" /> -->


        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />

        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="update" />

        <property name="hibernate.c3p0.min_size" value="5" />
        <property name="hibernate.c3p0.max_size" value="10" />
        <property name="hibernate.c3p0.timeout" value="1800" />
        <property name="hibernate.c3p0.max_statements" value="50" />

    </properties>
</persistence-unit>

</persistence>

Java request.

   package br.com.rreefstore.model.entity;

   import java.util.Calendar;

   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 javax.persistence.UniqueConstraint;

   /**
    * @author Tiago Ferezin
    *
    */
   @Entity
   @Table(uniqueConstraints = { @UniqueConstraint(columnNames = "codigoPedido", name = "uk_codigoPedido") })
   public class Pedido extends AEntity<Pedido> {

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

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
private Long idPedido;

@Column(nullable = false)
private String codigoPedido;

@Column(nullable = false)
private Integer quantidade;

@Column(nullable = false)
private Calendar dataCriacao;

private Calendar dataDesativacao;

public Pedido() {

}

/**
 * @return the idPedido
 */
public Long getIdPedido() {
    return idPedido;
}

/**
 * @param idPedido the idPedido to set
 */
public void setIdPedido(Long idPedido) {
    this.idPedido = idPedido;
}

/**
 * @return the codigoPedido
 */
public String getCodigoPedido() {
    return codigoPedido;
}

/**
 * @param codigoPedido the codigoPedido to set
 */
public void setCodigoPedido(String codigoPedido) {
    this.codigoPedido = codigoPedido;
}

/**
 * @return the quantidade
 */
public Integer getQuantidade() {
    return quantidade;
}

/**
 * @param quantidade the quantidade to set
 */
public void setQuantidade(Integer quantidade) {
    this.quantidade = quantidade;
}

/**
 * @return the dataCriacao
 */
public Calendar getDataCriacao() {
    return dataCriacao;
}

/**
 * @param dataCriacao the dataCriacao to set
 */
public void setDataCriacao(Calendar dataCriacao) {
    this.dataCriacao = dataCriacao;
}

@Override
public Long getId() {
    // TODO Auto-generated method stub
    return idPedido;
}

@Override
public void setId(Long id) {
    // TODO Auto-generated method stub
    this.idPedido = id;
}

@Override
public boolean isDeleted() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public Calendar getDataDesativacao() {
    // TODO Auto-generated method stub
    return dataDesativacao;
}

@Override
public void setDataDesativacao(Calendar dataDesativacao) {
    // TODO Auto-generated method stub
    this.dataDesativacao = dataDesativacao;
}

}

To EntityManager wheel correctly, and is not null.

When I run the insertion method the following error appears:

Caused by: org.postgresql.util.Psqlexception: ERROR: relation "request" does not exist

Problem that even before creating this class Pedido.java, the whole system was running normal, the bank updated the tables and the ones that did not have, was created automatically by Hibernate, and now it does not create or update any table.

Stacktrace below, remembering that no class relates to Pedido.java.

Hibernate: 
    insert 
    into
        Pedido
        (dataCriacao, dataDesativacao, quantidade) 
    values
        (?, ?, ?)
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1187)
    at br.com.rreefstore.dao.GenericDAO.create(GenericDAO.java:60)
    at br.com.rreefstore.dao.factory.GenericDAOFactory.create(GenericDAOFactory.java:26)
    at br.com.rreefstore.test.model.entity.PedidoTestNG.criacao(PedidoTestNG.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:84)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181)
    ... 27 more
Caused by: org.postgresql.util.PSQLException: ERROR: relation "pedido" does not exist
  Posição: 13
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    ... 48 more
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1187)
    at br.com.rreefstore.dao.GenericDAO.create(GenericDAO.java:60)
    at br.com.rreefstore.dao.factory.GenericDAOFactory.create(GenericDAOFactory.java:26)
    at br.com.rreefstore.test.model.entity.PedidoTestNG.criacao(PedidoTestNG.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:84)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181)
    ... 27 more
Caused by: org.postgresql.util.PSQLException: ERROR: relation "pedido" does not exist
  Posição: 13
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    ... 48 more

How to solve this?

Remembering that this error occurs with all the classes I create after the first run, I created Pessoa.java and pointed out:

Caused by: org.postgresql.util.Psqlexception: ERROR: relation "person" does not exist

What solution to the problem?

PS: Code available in:

Project available on Github

  • There was no answer

  • 1

    Is there any class that has a relationship with the class Pedido? If there is, add it and place the stack trace whole please.

  • @Felipemarinho put the stack trace in the advertised above, edited the question to put more details, and there is no class that has the relationship with Pedido please help

  • 1

    Maybe Hibernate is creating the tables with the uppercase initial and time to execute any other statement is using lower case letters. Check if the tables are uppercase in the database. If they are, try to enter in the annotation @Table the name tables with lower case letter.

  • @Felipemarinho, unfortunately I have the bank created, but the Hibernate neither creating the tables, I did the information of the @Table(name="pedido") in class Pedido.java and the error persists.

  • Isn’t he creating any table? In this case he should throw some exception indicating the reason at the time of your application startup.

  • I’m running on Testng and you’re giving this problem and not really creating any table, actually I was creating the right tables, but stopped all of a sudden and started this error @Felipemarinho

  • In which folder the persistence.xml is located? I see that you are running it in a test context, so it may be that Hibernate is looking for it in the wrong folder.

  • @Victorstafusa the persistence.xml is located in the folder src/main/resources/META-INF/

  • You can get past the comm DDL of the creation of the requested table in Postgre?

  • @Tiagoferezin Try to copy and paste it (by duplicating, not to erase the original) to src/test/resources/META-INF/.

  • @Anthonyaccioly, I didn’t understand your question, could you rephrase it? Grateful

  • @Victorstafusa, I followed what you asked and pointed out another mistake java.lang.IllegalArgumentException: Unknown entity: br.com.rreefstore.model.entity.Pedido

  • @Tiagoferezin, I’m asking you to update the question with the command CREATE TABLE + code creating any constraints, e. g, uk_codigoPedido, to Primary Key, table etc Pedido. I am suspicious that your table was not created, or was created in the wrong way. hibernate.hbm2ddl.auto=update has its problems. If it’s a local environment and the data doesn’t matter to you maybe it’s worth trying with hibernate.hbm2ddl.auto=create-drop (that will drop your schema, then be careful)

  • @Anthonyaccioly, actually Hibernate is not really creating the tables, if you enter the bank you will only have the previous tables and the create-drop is not interesting to me because it is an Ecommerce

  • Tenteo create-drop in a new schema in the database just to see if the table is properly created. hibernate.hbm2ddl.auto=update is also not desirable for environments other than the developer’s machine. You will soon need a policy of tracking / migration / data evolution and bump into tools like Liquibase or Flyway as more robust alternatives to the Hibernate ddl command generation engine.

  • @Anthonyaccioly create-drop is running normal, it creates and drops the tables, the update it creates the tables only the first time, the second time, if I delete a table, it does not update and create it again.

  • Hi James, if the create-drop is creating the table pedido as expected then you really have no problem in the above code, the problem is the policy of update. I understand that your expectation with this question was to give a survival to the simplicity of the hbm2ddl.auto but unfortunately the best advice I have to give you is to migrate to a more robust solution like Liquibase or Flyway. It’s worth doing it while the schema is still fairly small.

  • @Anthonyaccioly and how I would implement the Flyway in my project?

  • 1

    Hello James, this is worth a new question. But in a very short way, you will export all your SCHEMA to an SQL file. This will be the first "migration". Henceforth each modification in your entities should be accompanied by a new "migration" (SQL file changing columns, adding new tables, etc.). Flyway keeps a history of applied migrations in each environment and makes the evolution process ’s easy schema. From a look at documentation to get an idea of what this involves.

  • @Anthonyaccioly posed as a question that question in link

Show 17 more comments

1 answer

1


In your file hibernate.cfg.xml, check the line:

<property name="hbm2ddl.auto">validate</property>

Options:

  • validate: validate the schema, make no changes to the database.
  • update: update the schema.
  • create: creates the schema, destroying previous data.
  • create-drop: drop the schema when the session ends.

Reference

  • I am not using Hibernate.cfg.xml, the settings are all in persistence.xml, and ran normal, the company that worked also did not use Hibernate.cfg.xml and we had some web applications running normal.

  • And this configuration is already in the persistence.xml , as you can see <property name="hibernate.hbm2ddl.auto" value="update" />

  • That answer didn’t solve the problem, my friend

Browser other questions tagged

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