简体   繁体   English

JPA实体的JAXB Factory

[英]JAXB Factory for JPA entities

In most of my previous projects I have two domain models, one with JAXB annotations and the other one with JPA annotations. 在我以前的大多数项目中,我都有两个域模型,一个带有JAXB注释,另一个带有JPA注释。 I know they can combined into one model with both annotations in the same class, but in my experiences the tradeoffs with this approach always came to the conclusion to separate them. 我知道它们可以组合成一个模型,并且在同一个类中具有两个注释,但是根据我的经验,总是会得出将这种方法分开的结论。 Another advantage of a separate approach is the ability to create the JAXB classes with a XSD and easily link in XSDs from other projects. 单独方法的另一个优点是可以使用XSD创建JAXB类,并可以轻松地从其他项目链接到XSD中。

In most cases I need factory classes being able for flexible creation of JAXB representations of my entities, eg 在大多数情况下,我需要工厂类能够灵活地创建实体的JAXB表示形式,例如

public class UserFactory
{
  public UserFactory(User queryUser, String lang)
  {
     this.queryUser=queryUser;
     this.lang=lang;
  }

  public JaxbUser getUser(JpaUser jpaUser)
  {
     JaxbUser jaxbUser = new JaxbUser();

     if(queryUser.isSetId()){jaxbUser.setId(jpaUser.getId());}
     if(queryUser.isSetEmail()){jaxbUser.setEmail(jpaUser.getEmail());}

     if(queryUser.isSetRoles())
     {
       RolesFactory f = new RolesFactory(queryUser.getRoles(),lang);
       jaxbUser.setRoles(f.getRoles(jpaUser.getRoles()));
     }
     return jaxbUser;
  }
}

I create a UserFactory with an individual template queryUser and the desired lang for entities supporting different languages. 我使用单独的模板queryUser和支持不同语言的实体所需的lang创建一个UserFactory。 The template is checked during creation of the result for specific fields or additional factories and the resulting object is created. 在针对特定字段或其他工厂的结果创建期间检查模板,并创建结果对象。 The query is defined in a XML file like this: 查询是在XML文件中定义的,如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<query lang="en">
  <user id="1">
    <roles>
      <role code="code"/>
    </roles>
  </user>
</query>

With this methodology I have a powerful and flexible tool to create customized XML, despite all drawback of maintaining two domain models and the factory classes. 尽管保留了两个域模型和工厂类的所有缺点,但通过这种方法,我拥有一个强大而灵活的工具来创建自定义XML。 I know there are many frameworks or libraries available which I never have heard about, so here my question: 我知道有很多我从未听说过的可用框架或库,所以这里是我的问题:

Is there something available similar to my approach? 是否有类似我的方法的可用方法?

There are basically two options: 基本上有两种选择:

  • JPA and JAXB annotations on the same classes (see Hyperjaxb3 or DataNucleus ) 同一类上的JPA和JAXB批注(请参见Hyperjaxb3DataNucleus
  • Or you keepm the separated and write code to map one onto another 或者,您保持分离并编写代码以将一个映射到另一个

I personally do not see much added value in the cross-model mapping code. 我个人认为跨模型映射代码没有太多附加值。 Usage of factories also does not seem too innovative, it is just a question of programming technique which you use to map one onto another. 工厂的用法似乎也不太创新,这只是编程技术的问题,您可以使用编程技术将它们相互映射。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM