vladmihalcea / hypersistence-optimizer

Hypersistence Optimizer allows you to get the most out of JPA and Hibernate. By scanning your application configuration and mappings, Hypersistence Optimizer can tell you what changes you need to do to speed up your data access layer.
https://vladmihalcea.com/hypersistence-optimizer/
Apache License 2.0
311 stars 43 forks source link

OneToOneWithoutMapsIdEvent is not applicable when parent and child are the same class/table #243

Open pkernevez opened 4 months ago

pkernevez commented 4 months ago

Hi I have chains with MovementEntities with a OneToOne relation.

public class MovementEntity {
...
    @OneToOne(optional = true, fetch = FetchType.LAZY)
    @JoinColumn(name = "PARENT_MOVEMENT_ID")
    private MovementEntity parentClientSecurityMovement;

The optimizer show me this error: Hypersistence Optimizer : CRITICAL - OneToOneWithoutMapsIdEvent - The [parentClientSecurityMovement] one-to-one association in the [tech.lightframe.shift.cbs.operation.MovementEntity] entity is using a separate Foreign Key to reference the parent record. Consider using @MapsId so that the identifier is shared with the parent row. For more info about this event, check out this User Guide link - https://vladmihalcea.com/hypersistence-optimizer/docs/user-guide/#OneToOneWithoutMapsIdEvent

But this strategy is not applicable when parent and child are in the same table. They are different instances and can't have the same Id. I think the optimizer should filter these cases.

vladmihalcea commented 4 months ago

From JPA perdpective, that's a @ManyToOne association. If you replace it with @ManyToOne, it will work just fine.