Query returns subclasses in Spring

Asked

Viewed 63 times

0

I’m having trouble mapping the classes of a spring project I’m working on. I have the super class called Itempauta and the subclass Homologation.

Pai Class

@Data
@Entity
@Table(name="item_pauta")
@AttributeOverrides({
@AttributeOverride(name="id", column=@Column(name="ite_id"))
})
@EqualsAndHashCode(callSuper = true)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@DiscriminatorValue("itemPauta")
public class ItemPauta extends BaseEntity {

@OneToMany(cascade = CascadeType.ALL,
                fetch = FetchType.LAZY,
                mappedBy = "itens",targetEntity = Objeto.class)
    List<Objeto> objetos;


//outros campos aqui 


}

daughter class :

@Data
@Entity
@Table(name="homologacao")
@AttributeOverrides({
    @AttributeOverride(name="id", column=@Column(name="hom_id"))
})
@EqualsAndHashCode(callSuper = true)
@Polymorphism(type = PolymorphismType.EXPLICIT)
public class Homologacao extends ItemPauta {

@Column(name="hom_tds")
@Size(max=20)
String numeroTDS;

}

In this configuration the system creates two tables and when searching in the parent class returns tmb records of the daughter class. I have tried with Inhereted.Joined, but tmb returned the records of the daughter class, in the daughter class I put the annotation Explicit and tmb discriminatorValue, but tmb did not get result.

Here the query call :

@Override
@Transactional(readOnly = true)
public List<ItemPauta> findaAll() {
    return repository.encontrarTodos();
}

class Itempautarepository

@Query("select itens from ItemPauta itens")
 public List<ItemPauta> encontrarTodos () ;

1 answer

1

Go to your Omain class and put the @Jsonignore annotation in the attribute that you don’t want to return when you do the search. If it doesn’t work instead of putting in the attribute put in the get method, for example:

@JsonIgnore
public String getNome(){
    return this.nome;
}

Package import:

import com.fasterxml.jackson.annotation.JsonIgnore;
  • Hello Fabio, now that I read your reply thank you. I even gave up doing it this way, I decided to make one more daughter class and from her do the research: so it will be in the other subclasses, I will not research more in the parent class. Thanks anyway, I’ll try what you suggested.

Browser other questions tagged

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