ERROR: column "type" of relation "tb_person" does not exist

Asked

Viewed 1,539 times

1

Hello.

I am trying to persist a record, but when I run, it is returning the error below:

// ...
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "tipo", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("P")
public class Pessoa {   
    private Integer id;
    private String nome;
}
// ...
@DiscriminatorValue(value = "F")
@PrimaryKeyJoinColumn(name = "id")
public class PessoaFisica extends Pessoa {   
    private String cpf;
}
@Stateless
public class PessoaFisicaDAO {
    @TransactionAttribute(TransactionAttributeType.MANDATORY)
    public void incluirPessoa(Pessoa pessoa) {
        em.persist(pessoa);
    }
}
<?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">

<persistence-unit name="teste">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jboss/datasources/testeDS</jta-data-source>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
        <!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
        <!-- <property name="hibernate.show_sql" value="true" /> -->
        <!-- <property name="hibernate.format_sql" value="true" /> -->
        <!-- <property name="hibernate.generate_statistics" value="true" /> -->
        <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
    </properties>
</persistence-unit>

08:17:24,371 INFO [org.hibernate.hql.internal.QueryTranslatorFactoryInitiator] (default task-10) HHH000397: Using ASTQueryTranslatorFactory
08:17:50,974 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-11) SQL Error: 0, SQLState: 42703
08:17:50,976 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-11) ERROR: column "tipo" of relation "tb_pessoa" does not exist
Posição: 52

Thank you.

  • What database are you using?

  • Postgres... Thank you

  • Are you creating your tables by Hibernate itself through Hibernate.hbm2ddl.auto? If you can put your Hibernate configuration file it would be of great help.

  • Include the persistence.xml. It’s exactly the way I posted it. Even with the comments.

1 answer

0

As indicated by the error itself, there is no column with the name tipo on the table tb_pessoa. Check if they exist in your database, since they are not being created by Hibernate due to property hibernate.hbm2ddl.auto be commented.

If they exist in the database, another possibility is the difference of case between the name of this column/table in the database and the name being used for the query, since Postgresql changes to lowercase all names that are not in quotes. You can try to change the name of your columns and tables to lowercase, both in the database and in their entities.

Another possibility is to activate the Hibernate property hibernate.globally_quoted_identifiers for the case of tables/columns to be kept as those defined in their classes. If you do, remember to remove the property’s comment tag hibernate.hbm2ddl.auto for Hibernate to update its tables.

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

One last remark, the class org.hibernate.dialect.PostgreSQLDialect is marked as obsolete. If your Postgresql version is 8.2 or higher, change your Dialect to the corresponding version of your version:

org.hibernate.dialect.PostgreSQL82Dialect
org.hibernate.dialect.PostgreSQL9Dialect
org.hibernate.dialect.PostgreSQL91Dialect
org.hibernate.dialect.PostgreSQL92Dialect
org.hibernate.dialect.PostgreSQL93Dialect
org.hibernate.dialect.PostgreSQL94Dialect
org.hibernate.dialect.PostgreSQL95Dialect

Browser other questions tagged

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