将JPA信息与POJO分开 [英] Separating JPA information from POJO

查看:89
本文介绍了将JPA信息与POJO分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,该项目将使实体使用JPA持久化到数据库中.我们将使用Maven作为项目管理框架.我想知道是否有可能为POJO创建一个项目,为持久性定义创建另一个项目,然后将两者组合"为一个包含POJO及其持久性信息的输出.

I am working on a project that will have entities persisted to a database using JPA. We will be using Maven as the project management framework. I am wondering if it would be possible to create one project for the POJOs and another for the persistence definitions and then "combine" the two into a single output that contains the POJOs and their persistence information.

基本上,我正在尝试将POJO的代码与持久性定义分开.因为POJO可能会被几个不同的项目重用,这些项目可能会或可能不需要持久化它们,并且可能会或可能不想更改持久性信息. (与是否可以通过扩展POJO来构建JPA实体?)

Basically I am trying to separate the code that POJOs from the persistence definition. Because the POJOs may be reused by several different projects that may or may not need to persist them and may or may not want to change the persistence information. (Similar but not quite the same as Is it possible to build a JPA entity by extending a POJO?)

我对如何做到这一点有两个想法.如果要在Web应用程序中使用POJO,则可以提供persistence.xml并映射该项目中的类,并仅向包含POJO的项目添加依赖项.但是,如果我想创建一个包含持久性信息和POJO的jar文件,我想我可以使用shade插件吗?

I have two ideas on how I might be able to do it. If I were to use the POJOs in a web application I could provide persistence.xml and map the classes in that project and just add a dependency to the project containing the POJOs. But if I wanted to create a single jar file containing the persistence information and the POJOs, I think I could use the shade plugin?

是否有其他方法可以将两个Maven项目本质上合并为一个输出,这是一件很合理的事情吗?

Is there any other way to essentially merge two maven projects into a single output and is this a reasonable thing to want to do?

推荐答案

我非常感谢您的投入.最后,对我来说解决方案是创建两个项目.第一个提供了没有任何JPA信息的POJO的定义.是的,有一些与JPA相关的成员,例如id,但我会解决这些问题.第二个项目包含JPA元数据(orm和持久性XML文件).

I do appreciate the input. In the end, the solution for me was to create two projects. The first provided the definition of the POJOs without any JPA information. Yes there are some JPA related members such as id but I will address those. The second project contained the JPA metadata (orm and persistence XML files).

对于与持久性相关的成员(例如id),我可能可以与模型类中的成员同住,但可以使用本文中的建议(

As for the members related to persistence (e.g. id) I could probably live with those in the model classes but using the suggestion in this post (Is it possible to build a JPA entity by extending a POJO?) I extended my POJO classes and declared id in the "entity" sub classes. This does require some consideration when defining the POJO in terms of access to members.

值得注意的是,当您具有类层次结构(模型中的继承)时,此解决方案会遇到麻烦. 纯"模型中的类继承自一些通用类.然后,在持久性"模型中扩展该类,以提供id和其他与持久性相关的成员.现在,如果持久子类继承自纯"模型中的类,则它们不会继承id和其他持久成员.

One thing to note, this solution runs into trouble when you have a class hierarchy (inheritance in your model). The classes in your "pure" model inherit from some common class. That class is then extended in the "persistence" model to provide the id and other persistence related members. Now if the persistent subclasses inherit from the classes in the "pure" model, they do not inherit the id and other persistent members.

在不同的继承映射中可能有变通办法,例如每个具体类的表.

There may be workarounds in different inheritance mapping such as table per concrete class.

这篇关于将JPA信息与POJO分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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