我可以在Discriminated Entity Hierarchy的子类上使用CompositeId吗? [英] Can I use CompositeId on subclasses of Discriminated Entity Hierarchy?

查看:248
本文介绍了我可以在Discriminated Entity Hierarchy的子类上使用CompositeId吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体层次结构有单个表的所有子类的策略。我想要使​​用每个属性映射的@ManyToOne作为主键。当我把@Id注释,我得到这个错误:

java.lang.ClassCastException:org.hibernate.mapping.SingleTableSubclass不能转换为org.hibernate。 mapping.RootClass



那么我该如何提供将所有属性定义为区分层次结构上的标识符?

解决方案

您的ID对于实体层次结构中的最高级对象而言必须是唯一的。你不能在子类上定义ID并且拥有一个没有ID的超类,也就是一个实体。



想想这样, entityManager.find(SuperClass.class,2); 是一个完全合法的调用。如果ID在子类中定义为FK列,那么它们中的多个ID可能为ID 2!什么会被返回?



使用ManyToOne关系的FK端作为主键首先是无意义的。按照定义,它将成为OneToOne。


I have a entity hierarchy has single table for all subclasses strategy. And I want to use each properties - mapped @ManyToOne - as primarykey. When I put @Id annotation, I'm getting this error:

java.lang.ClassCastException: org.hibernate.mapping.SingleTableSubclass cannot be cast to org.hibernate.mapping.RootClass

So how can I provide to define all properties as identifier on discriminated hierarchy?

解决方案

Your ID needs to be unique for the highest object in the hierarchy that is an entity. You can't define ID on subclasses and have an superclass, that is an entity, that has no ID.

Think of it this way, entityManager.find(SuperClass.class, 2); is a perfectly legal call. If ID were defined on subclasses as an FK column, more than one of them could have ID 2! What would be returned?

Using the FK side of a ManyToOne relationship as a primary key is nonsensical in the first place. It would by definition be a OneToOne then.

这篇关于我可以在Discriminated Entity Hierarchy的子类上使用CompositeId吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆