简体   繁体   中英

Mapping array column of ids for another entity in Hibernate

Say I have the following data in Postgresql table entity_b, which has an array column of ids from table entity_a:

id | e_a_ids[]
---+----------
b1 | a1,a2,a3
b2 | a1,a3

I want to map in Hibernate these two entities with annotations:

@Entity
class EntityA {
    Set<EntityB> entityBs;
}

@Entity
class EntityB {
    Set<EntityA> entityAs;
}

I have found examples of custom hibernate type that works with array column, but those are for primitives not for another entity type. I can also use a normal id column instead of an array column with the traditional many to many method by introducing another mapping table, but thats not so much a question to ask here then.

How can a bidirectional mapping with an array column of ids for another entity be mapped in Hibernate for a Postgresql database tables?

Even if there would be ways to make this work with Hibernate (which may be hard as @Thomas suggested), you would most probably loose the foreign key constraint validation.

Neither postresql nor mysql (just 2 examples) seem to support this:
postgresql: PostgreSQL array of elements that each are a foreign key
mysql: Array of foreign keys in one column

So it might be best to stick with the standard association table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM