使用JPA保留第三方对象 [英] Persisting 3rd party objects with JPA

查看:88
本文介绍了使用JPA保留第三方对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我目前的项目中,我使用的是没有JPA注释的第三方库。

In my current project I am using a 3rd party library which has no JPA annotations.

如何使用JPA和外部映射来保存来自该库的对象?

How can I persist objects from that library using JPA and external mappings?

推荐答案

检查这个这个。简而言之:


  1. 创建 META-INF / orm.xml

  2. 关注(阅读) .xsd

  1. Create META-INF/orm.xml
  2. Follow (read) the .xsd

您不必手动映射每列 - 只需要一些细节(即集合和ID)。假定所有字段都是列(如果映射了类)。如果没有集合,这样的东西就足够了:

You don't have to manually map each column - only some specifics (i.e. collections and the id) are required. All fields are assumed to be columns (if the class is mapped). If there are no collections, something like this suffices:

<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm 
    http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
    version="1.0">

    <description>External entities from library X</description>
    <package>com.external.library</package>
    <entity class="SomeClassName">
      <id>..</id>
    </entity>
    <entity class="AnotherClassName">
      <id>..</id>
    </entity>
</entity-mapping>

请注意,当指定< package> 时您不需要完全限定名称。

Note that when specifying <package> you don't need fully-qualified names.

如果您想要一个名称与 orm.xml不同的文件 ,在 persistence.xml 中通过以下方式指定:

In case you want a file named differently than orm.xml, in your persistence.xml specify it via:

<mapping-file>customMappingFile.xml</mapping-file>

这篇关于使用JPA保留第三方对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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