Search code examples
mysqlhibernatejoinormhibernate-mapping

Hibernate create relation without relational table


Is it possible to map hibernate entities without using another table that is mapping them ?

When I create @OneToMany and @ManyToOne relation between 2 entities, hibernate always creates another table in the database to map the relation, I would like to map 2 entities directly using column in the mapped entity like this:

enter image description here

"InstalledApp" entity:

@OneToMany (fetch = FetchType.EAGER, targetEntity=InstalledAppPort.class, mappedBy = "id")
private List<InstalledAppPort> ports;

"InstalledAppPort" entity:

@ManyToOne (targetEntity=InstalledApp.class, fetch=FetchType.LAZY)
@JoinColumn(name = "iappId")
private InstalledApp iapp;

When using the code above, the list is always empty and I do not know why. Technically this mapping should work but it is not. No exception thrown.


Solution

  • Solved by using:

    @OneToMany (fetch = FetchType.EAGER, targetEntity=InstalledAppPort.class)
    @JoinColumn(name = "iappId", referencedColumnName = "id")
    private List<InstalledAppPort> ports;