[英]mapping superclasses and subclasses with hibernate (JPA)
I am trying to persist objects in a database using hibernate JPA. 我正在尝试使用休眠JPA将对象持久存储在数据库中。
The objects already have a type hierarchy, and I'm trying to make it work with hibernate. 这些对象已经具有类型层次结构,我正在尝试使其与休眠模式一起使用。
A CatalogPackage
object has all the important properties and all the getters. CatalogPackage
对象具有所有重要的属性和所有的吸气剂。 A CatalogPackageImpl
( extends CatalogPackage
) object has no properties, but most of the setters. CatalogPackageImpl
( extends CatalogPackage
)对象没有属性,但是大多数设置器都具有属性。
Both classes are non-abstract. 这两类都是非抽象的。
We want code to refer to CatalogPackage
objects. 我们希望代码引用
CatalogPackage
对象。 But when initializing hibernate, it complains about the setters missing from the CatalogPackage
class. 但是,在初始化休眠模式时,它会抱怨
CatalogPackage
类中缺少设置器。
How do I suggest to hibernate that it use the subclass when building the objects? 我如何建议在构建对象时使其休眠使用子类?
I don't want to move all the setters to the superclass, and I don't want to use CatalogPackageImpl
as the entity. 我不想将所有的setter移到超类,也不想使用
CatalogPackageImpl
作为实体。
Even though I can't see the problem with defining the setter methods in your CatalogPackage
since they can be marked private
to avoid using them from external world. 尽管我看不到在
CatalogPackage
定义setter方法的问题,因为可以将它们标记为private
以避免从外部环境使用它们。 Since you didn't paste your entities configuration and that you say Hibernate is complaining about the setter methods I can conclude you are using your getters to describe your entity mapping, right? 因为您没有粘贴您的实体配置,并且您说Hibernate在抱怨setter方法,所以我可以得出结论,您正在使用getter来描述您的实体映射,对吗?
In such a case Hibernate will always still complaining because it assumes anything mapped to the database should be done in both directions , if it can read from the datastorage so it should be allowed to write in there. 在这种情况下,Hibernate总是会抱怨,因为它假设映射到数据库的任何事情都应该在两个方向上进行 ,如果它可以从数据存储中读取数据,那么应该允许它在其中进行写入。
SO you have either of below solutions: 因此,您有以下两种解决方案之一:
Move the mapping from getter methods to fields and set the acces type to field on top of your entity: 将映射从getter方法移动到字段,并将acces类型设置为实体顶部的field:
@Access(AccessType.Field) class CatalogPackage { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.