JPA/Hibernate:我可以有一个没有注释的父级和一个有注释的子级吗? [英] JPA/Hibernate: Can I have a Parent without annotations and a Child with

查看:24
本文介绍了JPA/Hibernate:我可以有一个没有注释的父级和一个有注释的子级吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个可以独立使用或在更大的应用程序中使用的组件.独立使用时,它不需要数据库,也不需要任何与数据库/ORM 相关的依赖项.当用作较大应用程序的一部分时,某些类确实会通过 JPA/Hibernate 持久化到数据库中.

I have a requirement to create a component which can be used stand-alone or within a larger application. When used stand-alone, it should not require a database, nor any database/ORM related dependencies. When used as as part of a larger app, some of the classes will indeed be persisted to a database via JPA/Hibernate.

我认为组件中的域对象不会有任何注释(因为要求没有任何与 ORM 相关的依赖项).在更大的应用程序中,我会将这些域对象进行子类化......这大约是我所做的.

I was thinking the domain objects in the component would not have any annotations (due to the requirement to not have any ORM-related dependencies). In the larger app, I would then sub-class those domain objects...which is about as far as I've made it.

通常,我使用字段级注释.是否有可能与上述场景有关?我想不是.

Typically, I use field-level Annotations. Is it possible to due with with the scenario described above? I'm thinking it's not.

另一个认为我必须切换到注释属性.在这种情况下,我会扩展未注释的域对象,覆盖所有属性,&注释那些.孩子只是具有所需注释的代表.这看起来像是很多工作/代码.

The other thought I had to switch to annotating the properties. In which case, I'd extends the non-annotated domain object, override all the properties, & annotate those. The child would just be a delegate with the required annotations. This just seem like a lot of work/code.

具有讽刺意味的是,我认为如果我使用 hbm.xml,这将更容易实现,我们最近已经放弃了.我是否遗漏了一些带有注释的东西?

Ironically, I think this would be more easily doable if I was using hbm.xml, which we've recently moved away from. Am I missing something with annotations?

推荐答案

JPA 注释确实是您唯一的依赖项.如果有人以 POJO(非 DB)方式使用您的对象,他们所需要的只是 JPA 注释 jar,对于大多数应用程序服务器来说,无论如何它实际上都在类路径上.

The JPA annotations are really your only dependency. If somebody is using your object in a POJO (non DB) way that all they will need is the JPA annotations jar, which for a most application servers is actually on the classpath anyhow.

当然只是将注解作为依赖是很合理的,这不像你需要拖入整个hibernate而且它有很多朋友.

Surely just having the annotations as a dependency is pretty reasonable, it's no like you need to drag in the whole of hibernate and it's many friends.

另一种方法是,您已经确定对此实体使用 hbm.这还不错,因为 hibernate 很乐意在单个 SessionFactory 中混合注释和 hbm 样式的实体,而且您仍然可以使用字段访问,因此无需损害类的公共接口.

The alternative is as you've worked out is to use hbm's for this entity. It's not that bad, as hibernate is quite happy mixing annotation and hbm style entities in a single SessionFactory and you can still use field access so don't need to compromise the public interface to your class.

这篇关于JPA/Hibernate:我可以有一个没有注释的父级和一个有注释的子级吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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