Mapping with Inheritancetype.JOINED Does not work

Asked

Viewed 108 times

1

I’m trying to solve a college activity where specifications were passed to map the classes with JPA. I have the following inheritance to map: Employee (parent class), Seller (daughter) and Admistrative (daughter). At first I mapped using Inheritancetype.TABLE_PER_CLASS, worked and created the tables in the database. But the teacher wants us to use Inheritancetype.JOINED, and that’s where my problem is, I did the mapping following his example and it didn’t work, I tried several examples on the internet and it didn’t work. Below I’ll put the mapping I’ve done so far and the Hibernate error.

OBS.: For the Inheritancetype.TABLE_PER_CLASS and the Inheritancetype.SINGLE_TABLE works perfectly, the problem is to map with the Inheritancetype.JOINED;

Pai Class

@Entity(name = "tab_funcionario")
 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 @Table(uniqueConstraints = {
    @UniqueConstraint(name = "un_funcionario_rg", columnNames = { "rg",  "rg_orgao_expedidor", "rg_uf" }) }

public abstract class Funcionario {

@Id
@Column(name = "cpf", length = 11, nullable = false, columnDefinition = "CHAR(11)")
private String cpf;

@Column(name = "nome", length = 40, nullable = false, columnDefinition = "VARCHAR(40)")
private String nome;

@Column(name = "rg", length = 12, nullable = true, columnDefinition = "CHAR(12)")
private String rg;

@Column(name = "rg_orgao_expedidor", length = 20, nullable = true, columnDefinition = "VARCHAR(20)")
private String rgOrgaoExpedidor;

@Column(name = "rg_uf", length = 2, nullable = true, columnDefinition = "CHAR(2)")
private String rgUf;

@ElementCollection()
@CollectionTable(name = "tab_funcionario_telefones", joinColumns = @JoinColumn(name = "cpf"))
@Column(name = "telefone", length = 12, nullable = false, columnDefinition = "VARCHAR(12)")
private List<String> telefone = new ArrayList<>();

@Temporal(TemporalType.DATE)
@Column(name = "data_nascimento", nullable = false, columnDefinition = "DATE")
private Date dataNascimento;

@Embedded()
@Column(name = "endereco", nullable = false)
private Endereco endereco;

Classes Daughters

@Entity(name = "tab_vendedor")
public class Vendedor extends Funcionario {

@Column(name = "percentual_comissao", nullable = false, precision = 10, scale = 2)
private BigDecimal percentualComissao;

@Convert(converter = SituacaoVendedorConverter.class)
@Column(columnDefinition = "char(3)", nullable = false)
private SituacaoVendedorEnum situacao;

@ManyToMany(cascade = CascadeType.PERSIST)
@JoinTable(name = "tab_vendedor_pessoa_juridica", joinColumns = {
        @JoinColumn(name = "cpf") }, inverseJoinColumns = { @JoinColumn(name = "cnpj") })
private List<PessoaJuridica> clientes = new ArrayList<>();


@Entity(name = "tab_administrativo")
public class Administrativo extends Funcionario {

@Column(name = "turno", nullable = false)
private Integer turno;

Error of the Hibernate

1    [main] ERROR org.hibernate.AssertionFailure  - HHH000099: an assertion failure occurred 
(this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): 
org.hibernate.AssertionFailure: Table public.tab_funcionario not found


3    [main] ERROR br.ucsal.bes20191.bd2.mapeamentojpa.atividade.Teste  - Erro: 
javax.persistence.PersistenceException: [PersistenceUnit: mapeamento-jpa] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:967)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at br.ucsal.bes20191.bd2.mapeamentojpa.atividade.Teste.main(Teste.java:14)


Caused by: org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.JoinedSubclassEntityPersister
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:112)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77)
at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:128)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:301)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:452)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889)
... 4 more


Caused by: org.hibernate.AssertionFailure: Table public.tab_funcionario not found
at org.hibernate.persister.entity.AbstractEntityPersister.getTableId(AbstractEntityPersister.java:5241)
at org.hibernate.persister.entity.JoinedSubclassEntityPersister.<init>(JoinedSubclassEntityPersister.java:433)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96)
... 9 more

Persistence.xml

<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">

    <persistence-unit name="mapeamento-jpa">

        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <properties>
            <!-- dados da conexao -->
            <property name="javax.persistence.jdbc.driver"
                value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:postgresql://localhost:5432/mapeamento-jpa" />
            <property name="javax.persistence.jdbc.user"
                value="postgres" />
            <property name="javax.persistence.jdbc.password"
                value="postgresql" />

            <!-- propriedades do hibernate -->
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.PostgreSQL94Dialect" />
            <property name="hibernate.default_schema" value="public" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
        </properties>
    </persistence-unit>
</persistence>
  • Would you kindly post your Hibernate configuration?

  • I added the content of persistence.xml

1 answer

1

I believe you’ve found the Issue HHH-10490

The recommendation would be to change the Hibernate configuration to use catalog instead of schema, you could change then of:

<property name="hibernate.default_schema" value="public" />

To:

<property name="hibernate.default_catalog" value="public" />

If mapped using @Table, should be amended to use catalog also, changing from:

@Table(name="FOO", schema="BASE")

To:

@Table(name="FOO", catalog="BASE")

This same bug has been reported here, here and here

  • I made the change and keeps coming up with the same mistake.

Browser other questions tagged

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