简体   繁体   English

在具体的JPA类之间共享Java实现的最佳方法?

[英]Best way to share Java implementation between concrete JPA classes?

I have about 10 different entities in my J2EE application that right now share the exact same implementation. 我的J2EE应用程序中大约有10个不同的实体,它们现在共享完全相同的实现。 They all inherit from the same generic abstract class that has been annotated as a @MappedSuperclass, but this class contains none of the implementation I have repeated in all the concrete subclasses. 它们都继承自已注释为@MappedSuperclass的同一泛型抽象类,但此类不包含我在所有具体子类中都重复过的实现。

If I could, I'd put all the various fields and collections on this abstract superclass and therefore put the implementation methods there too -- all in one place instead of 10. However, due to JPA restrictions I cannot add JPA annotations to generic fields or accessors. 如果可以的话,我会将所有各个字段和集合放在此抽象超类上,因此也将实现方法放在那里-全部放在一个位置而不是10个位置。但是,由于JPA限制, 我无法将JPA批注添加到通用字段或访问者。

While I normally favor delegation to implementation inheritance anyway, due to another JPA restriction that says you can't have an embedded entity with a collection the idea of using a delegate also won't work. 尽管我通常通常偏向于委托来实现继承,但是由于另一个JPA限制,即您不能在集合中包含嵌入式实体,所以使用委托的想法也行不通。

When I had only 3-4 of these entities and 2-3 methods, it wasn't a big deal, but now I have about 10 -- and about 7-8 methods each...and some of the methods are getting really complex. 当我只有3-4个这样的实体和2-3个方法时,这没什么大不了的,但是现在我只有大约10个-大约有7-8个方法...而且其中一些方法正在变得越来越复杂。 And the "cut-copy-paste" inheritance I am using really sucks. 我正在使用的“剪切复制粘贴”继承确实很糟糕。

Any other brilliant ideas out there? 还有其他好主意吗?

  1. Double-check whether these "restrictions" actually hold true for your JPA provider. 仔细检查这些“限制”是否真正适用于您的JPA提供者。 I've had embedded objects with collections and it has been fine (with Hibernate). 我已经将对象嵌入到集合中,并且很好(使用Hibernate)。 And I've had a @MappedSuperclass with mapped fields. 我有一个带有映射字段的@MappedSuperclass

  2. You can try omitting the @MappedSuperclass , and make the superclass abstract and an @Entity with the proper inheritance hierarchy. 您可以尝试省略@MappedSuperclass ,并使用适当的继承层次结构@MappedSuperclassabstract一个@Entity

It turns out you can use either implementation inheritance or delegation as long as you are sure to use property access mode for your JPA annotations. 事实证明,只要您确保对JPA批注使用属性访问模式,就可以使用实现继承或委托。 I was using field access mode for JPA annotations and that was causing me no end of suffering because I could not annotate a generic field type. 我使用JPA注释的字段访问模式,这使我无尽痛苦,因为我无法注释通用字段类型。

However with property access mode I simply create my generic abstract implementation without annotating it as an entity, mappedsuperclass, embeddable, or anything. 但是,使用属性访问模式,我只创建了通用抽象实现,而没有将其注释为实体,mappedsuperclass,可嵌入或任何其他形式。 This way JPA will ignore it. 这样,JPA将忽略它。 Then, in the concrete subclasses, I create protected getter and setters methods as needed and put the JPA annotation on those. 然后,在具体的子类中,根据需要创建受保护的getter和setter方法,并将JPA注释放在这些方法上。

Turned out to be deceptively simple. 原来看似简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在两个或多个类之间共享同一接口实现的有效方法 - Efficient way to share the same interface implementation between two or more classes 在两个类之间共享公共代码的最佳方式 - Best way to share common code between two classes 在不同的JUnit测试类之间共享数据的最佳方法是什么 - What is the best way to share data between different JUnit test classes Java:在不同线程之间共享锁的最佳方法 - Java: best way to share Lock between different threads 在Java中,枚举值之间共享功能的最佳方法是什么? - In Java, what is the best way to share functionality between enum value? 有没有办法在抽象类和它的实现类之间共享数据而不使用 Kotlin 中的伴随对象 - Is there a way to share data between an abstract class and it's implementation classes without using companion object in Kotlin 在两个或多个项目之间共享公共代码(例如域类)的最佳方法是什么? - What is the best way to share common code (such as domain classes) between two or more projects? Java - 抽象类和具体类 - Java - Abstract classes and concrete classes 不使用单例的类之间共享数据的方法 - A way to share data between classes not using singleton JPA是如何找到具体实现调用的呢? - How does JPA find the concrete implementation to call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM