简体   繁体   中英

Spring JPA/ Hibernate : How to map polymorphic relation on non primary columns

class B{
@Any(metaColumn = @Column(name = "ITEM_TYPE"))
@AnyMetaDef(idType = "long", metaType = "string",
        metaValues = {
                @MetaValue(targetEntity = A.class, value = "A")
        })
@Cascade( { org.hibernate.annotations.CascadeType.ALL})
@JoinColumn(name = "ITEM_ID")
private A a;
...
...
}

I'm trying to Join table A and table B where B.item_type ='A' is constant and B.item_id= A.id .

It is throwing me

Caused by: org.hibernate.MappingException: Foreign key (FKi1uuph2wrvxtx66s7n7i1s09a:B [item_type,item_id])) must have same number of columns as the referenced primary key (A [id])

Any help on How shall i map this using spring jpa and hibernate?

Your question is not to clear but below may help

Join table A and table B where B.item_type ='A' is constant and B.item_id= A.id.

1) B.item_type = 'A' : Create A enum and pass it while querying 2) B.item_id=A.id :

lets says it is one to many relation.

Class B{

 @OneToMany
 @Cascade(org.hibernate.annotations.CascadeType.DETACH)
 @JoinColumns(@JoinColumn(name = "id", referencedColumnName = "item_id", insertable = false, updatable = false))
List<A> retunedList;

}

In case when it will be one to one, Change Annotaion to @onetoOne and make return type A instead of list.

我能够通过另一种方法来解决此问题,在该方法中,我将关联的实体设置为@Transient,然后通过添加EntityListener类在父实体的@PostPersist调用中保存了该瞬时实体。

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