Results contain both parent and child entity, when querying the parent

Hello!
I have a question, any help would be appreciated. I have the following entity:

public class CompetenceCategorySnapshot extends BasicEntity { 
	private static final long serialVersionUID = 1L;
	
	public static final String PROPERTY_NAME = "name";
	public static final String PROPERTY_OMSCHRIJVING = "description";
	public static final String PROPERTY_CATALOG = "catalog";
	
	private String name;	
	private String description;
	private CompetenceCatalogSnapshot catalog;
	private CompetenceCategorySnapshot parentCategory;
...

I have another entity that extends the previous:

public class CompetenceCategory extends CompetenceCategorySnapshot { 
	private static final long serialVersionUID = 1L;
	
	public static final String PROPERTY_COMPETENCES = "competences";
	public static final String PROPERTY_PARENT_CATEGORY = "parentCategory";
	public static final String PROPERTY_CATEGORY_CHILDREN = "categoryChildren";
	
	private Collection<Competence> competences = new ArrayList<Competence>();
	private Collection<CompetenceCategory> categoryChildren = new ArrayList<CompetenceCategory>();
	
	@JsonIgnoreProperties({ "competences", "categoryChildren" })
	private CompetenceCategory parentCategory;
...

I am trying to query the base entity:

    List<Integer> ids = Arrays.asList(2, 4);
    String queryText = "SELECT competencecategorysnapshot FROM CompetenceCategorySnapshot AS competencecategorysnapshot WHERE id in (:ids)";

    Query query = entityManager.createQuery(queryText);
    query.setParameter("ids", ids);

    query.getResultList().forEach(result -> {
        System.err.println(result);
    });

Output:

    CompetenceCategory[id=2,name=Taakgerichte competenties,description=...
    CompetenceCategory[id=4,name=Sociale competenties,description=...
    CompetenceCategorySnapshot[id=2,name=Taakgerichte competenties,description=...
    CompetenceCategorySnapshot[id=4,name=Sociale competenties,description=...

As you can see, I am getting the results for both the parrent and child entity, even though my query is only for CompetenceCategorySnapshot.

Both entities are mapped to the same table in the db. Here are the the hbm.xml files:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class 
    	name="com.brabo.training.core.competence.domain.catalog.CompetenceCategorySnapshot" 
    	table="competentie_categorie" 
    	schema="opleiding"
    	optimistic-lock="version">
...

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class 
    	name="com.brabo.training.core.competence.domain.catalog.CompetenceCategory" 
    	table="competentie_categorie" 
    	schema="opleiding"
    	optimistic-lock="version">
...

Am I doing something wrong?

Thanks in advance!

That’s just how ORMs work. They respect the polymorphism you define in your entities. If you want to just share common logic, then you need an intermediate class that is not an entity i.e. a mapped super class.

public class CompetenceCategorySnapshotCommon extends BasicEntity {
	private static final long serialVersionUID = 1L;
	
	public static final String PROPERTY_NAME = "name";
	public static final String PROPERTY_OMSCHRIJVING = "description";
	public static final String PROPERTY_CATALOG = "catalog";
	
	private String name;	
	private String description;
	private CompetenceCatalogSnapshot catalog;
	private CompetenceCategorySnapshot parentCategory;
...
}

public class CompetenceCategorySnapshot extends CompetenceCategorySnapshotCommon {}

public class CompetenceCategory extends CompetenceCategorySnapshotCommon { 
	private static final long serialVersionUID = 1L;
	
	public static final String PROPERTY_COMPETENCES = "competences";
	public static final String PROPERTY_PARENT_CATEGORY = "parentCategory";
	public static final String PROPERTY_CATEGORY_CHILDREN = "categoryChildren";
	
	private Collection<Competence> competences = new ArrayList<Competence>();
	private Collection<CompetenceCategory> categoryChildren = new ArrayList<CompetenceCategory>();
	
	@JsonIgnoreProperties({ "competences", "categoryChildren" })
	private CompetenceCategory parentCategory;
...

Thank you for the clear answer!