JoonT
JoonT

Reputation: 1246

jpa @ManyToOne multiple entity

I wanna make common MEMO entity.
Table schema is like below

CREATE TABLE MEMO(
  resource_id int,
  resource_type varchar(10),
  content varchar(255)
)

Every entities can have relationship to this MEMO entity.
resource_id can have a primary key from another entities,
it means resource_id is foreign key that allows all entities.
And resource_type is entity's type. Yes, this is like discriminator.

I don't wanna make entities like ITEM_MEMO, CATEGORY_MEMO, SCHEDULE_MEMO ...
I think it's good until now, but now I realize that I don't know how to implements this table using JPA.

As you know, @ManyToOne can have one object.
But in my case, resource_id should have relationshipt to many entities.

So... is there any way to do this?

Upvotes: 0

Views: 232

Answers (1)

crizzis
crizzis

Reputation: 10716

Why not use join tables instead? You could simply add @OneToMany @JoinTable private Collection<Memo> memos to all the relevant entities (the inverse association is, of course, impossible in this scenario).

Conversely, if you only need the @ManyToOne side, take a look at Hibernate's @Any annotation.

Upvotes: 2

Related Questions