'JPA Hibernate OneToOne children enity always fetches parent entity

I am having a problem with a OneToOne relationship. Trying to load a children entity will always result in an additional query that loads its parent, even if I don't need the parent.

I have already read to articles about it but can't figure it out:

https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/

https://thorben-janssen.com/hibernate-tip-lazy-loading-one-to-one/

My structure is quite similiar to their examples above. I've got a parent entity with a OneToOne relationship to a childen. My mapping is already unidirectional. So only the children has the mapping information and the parent has no children mappings at all. I am using the @MapsId annotation and have a shared PK/FK to the parent.

Short information about my entity structure:

@Entity
@Table(name = "parent")
public class ParentEntity 
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ParentSequence")
    private Long id;

@Entity
@Table(name = "children")
public class ChildrenEntity 
    @Id
    private Long id;
    @MapsId
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    private ParentEntity parent;

My issue is, that the children always fetches the parent eager and creating a second query for that reason.

Examples: #1 via Entity Manager find

entityManager.find(ChildrenEntity.class, parentId);

#2 via JPA Query

entityManager.createQuery("SELECT c FROM ChildrenEntity c").getResultList()

#3 via Criteria API

var query = cb.createQuery(ChildrenEntity.class);
var root = query.from(ChildrenEntity.class);
query.select(root).where(root.get(ChildrenEntity_.id).in(Arrays.asList(parent.getId())));
entityManager.createQuery(query).getResultList();

All three examples create more then one query. The additional query always load the whole parent entity.

Here a slightly cleaned up server output:

[0m[0m14:40:29,411 INFO  Hibernate: select childrenentity0_.parent_id, childrenentity0_.color, from children childrenentity0_ 
[0m[0m14:40:29,413 INFO  Hibernate: select parententity0_.id, parententity0_.additionalname from parent parententity0_ where parententity0_.id=?

Does anyone has an idea how to prevent loading of the parent entity? I do not really need it as the id is more then enough here.

I already found out, that I can use a .fetch statement in the criteria api. That helps reducing is to one query but that will add all parent fields to the select of the first query.

-- EDIT 22/05/09 --

Related Stackoverflow entry: JPA/Hibernate double select on OneToOne Find

Currently I only could find two possible solutions:

#1 Use a join fetch on ChildrenEntity to fetch the ParentEntity

This would work but joins a lot additional columns that are unneeded

#2 Do not select Entities but use a Tuple/Constructor query

-- EDIT 22/05/11 --

Looks like there is also a bug in various hibernate versions where the MapsId annotation does not work as expected.

https://hibernate.atlassian.net/browse/HHH-10771



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source