Reputation: 19635
I am currently trying to map multiple different tables to a single table, like this:
Person
| id | first_name | last_name |
| -- | ---------- | --------- |
| 1 | John | Doe |
Place
| id | place_name |
| -- | ---------- |
| 1 | White House |
Address
| id | ...typical address columns... | related_id | related_type |
| -- | ----------------------------- | ---------- | ------------ |
| 1 | address stuff | 1 | Person |
| 2 | more address stuff | 1 | Place |
I know how to represent this in the database obviously, so that's not the issue here. My issue is how do I represent the relationship in my Entity classes, as the typical @JoinColumn
annotation approach doesn't seem like it will work since we need both the related_id
and related_type
columns
@Entity
public class Person {
private String firstName;
private String lastName;
@OneToMany(fetch = FetchType.LAZY)
??? what to do here ???
private List<Address> addresses;
// omitted getters and setters
}
@Entity
public class Place {
private String placeName;
@OneToOne
??? what to do here ???
private Address address;
// omitted getters and setters
}
Is there any way to do this with JPA / Hibernate? I've found similar questions here but they are from ages ago and none of the solutions were really appropriate as they had more to do with inheritance.
Upvotes: 0
Views: 46