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

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

问题描述

我有个要求,创建其可以单独或更大的应用程序内使用的成分。当单独使用时,它不应该需要一个数据库,也没有任何数据库/ ORM相关的依赖性。当作为作为较大程序的一部分使用,一些类的确会持续到通过JPA /休眠的数据库。

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.

其他的想法,我不得不切换到注释的属性。在这种情况下,我扩展了非注释域对象,覆盖所有属性,&安培;注释这些。这孩子也只是在需要标注的委托。这只是看起来像很多工作/ code的。

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注解罐子,这对于一个大多数应用服务器实际上是对类路径进不去。

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.

当然,光有注解的依赖是pretty合理的,这是没有像你需要在整个休眠的拖动和它的许多朋友。

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 /休眠:我可以没有注释的父母和一个孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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