繁体   English   中英

如何在orm / persistence.xml中的持久性单元中为某些类设置混合模式

[英]How to set mixed schemas to certain classes in persistence unit in orm/persistence.xml

我正在开发一个JPA应用程序,该应用程序需要能够访问同一数据库(即DB2 / Oracle)上的不同模式。 我希望所有实体都注册在persistence.xml文件( <class> )中,并能够定义哪些实体属于某些模式。

我知道可以使用Java批注,但是这是一个需要最终用户对其进行相应配置的产品(因此,为什么我希望能够在xml文件中实现此功能)。

目前,我发现了这样的示例:

orm.xml

  <?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 orm_2_0.xsd"
    version="2.0">
    <persistence-unit-metadata>
        <persistence-unit-defaults>
            <schema>SECURITY</schema>
        </persistence-unit-defaults>
    </persistence-unit-metadata>
</entity-mappings>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="jpa.sample.plain">
        <mapping-file>custom-orm.xml</mapping-file>
        <class>com.reds.model.Role</class>
        <class>com.reds.model.User</class>
        <properties>
            <property name="javax.persistence.target-database" value="PostgreSQL" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://127.0.0.1:5432/security" />
            <property name="javax.persistence.jdbc.user" value="user" />
            <property name="javax.persistence.jdbc.password" value="password" />
            <!-- EclipseLink should create the database schema automatically -->
            <property name="eclipselink.ddl-generation" value="none" />
            <property name="eclipselink.weaving" value="false" />
            <!-- <property name="eclipselink.ddl-generation.output-mode"
                value="database" /> -->
            <property name="eclipselink.logging.level" value="INFO"/>
        </properties>     
    </persistence-unit>
</persistence>

在上述文件中,据我了解,在包含orm.xml映射文件的持久性单元上设置了模式“ SECURITY”(使其成为注释可以更改的默认值吗?)。 但这使persistence.xml中列出的两个类的模式都隐式为“ SECURITY”。

与上述相反,我需要:

  • 一种有效地在持久性单元中的类子集上列出架构的方法(除非唯一的方法是在orm.xml中手动进行实体映射,这是我希望避免的事情,最终用户也将避免这种情况) )
  • 如果可能,请使其远离供应商特定的xml文件或Java实现(请遵循JPA标准)
  • 附带问题:是否可以让JPQL查询透明地通过两个具有不同模式的实体?

项目详情:

  • Hibernate 5.1,JPA 2.1
  • DB2 / Oracle

编辑:

我发现此链接帮助我设置了orm.xml: https ://docs.jboss.org/hibernate/stable/annotations/reference/en/html/xml-overriding.html

以下是最相关的摘录:

3.1.2. Entity level metadata

You can either define or override metadata informations on a given entity.

<?xml version="1.0" encoding="UTF-8"?>

<entity-mappin(1)gs 
  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 orm_2_0.xsd"
  version="2.0">

    <package>o(2)rg.hibernate.test.annotations.reflection</package>
    <entity cl(3)ass="Administration" access="PROPERTY" metadata-complete="true">
        <table(4) name="tbl_admin">
            <unique-constraint>
                <column-name>firstname</column-name>
                <column-name>lastname</column-name>
            </unique-constraint>
        </table>
        <secon(5)dary-table name="admin2">
            <primary-key-join-column name="admin_id" referenced-column-name="id"/>
            <unique-constraint>
                <column-name>address</column-name>
            </unique-constraint>
        </secondary-table>
        <id-cl(6)ass class="SocialSecurityNumber"/>
        <inher(7)itance strategy="JOINED"/>
        <seque(8)nce-generator name="seqhilo" sequence-name="seqhilo"/>
        <table(9)-generator name="table" table="tablehilo"/>
        ...
    </entity>

    <entity class="PostalAdministration">
        <prima(10)ry-key-join-column name="id"/>
        ...
    </entity>
</entity-mappings>
1   entity-mappings: entity-mappings is the root element for all XML files. You must declare the xml schema, the schema file is included in the hibernate-annotations.jar file, no internet access will be processed by Hibernate Annotations.
2   package (optional): default package used for all non qualified class names in the given deployment descriptor file.
3   entity: desribes an entity.
metadata-complete defines whether the metadata description for this element is complete or not (in other words, if annotations present at the class level should be considered or not).

An entity has to have a class attribute refering the java class the metadata applies on.

You can overrides entity name through the name attribute, if none is defined and if an @Entity.name is present, then it is used (provided that metadata complete is not set).

For metadata complete (see below) element, you can define an access (either FIELD or PROPERTY (default)). For non medatada complete element, if access is not defined, the @Id position will lead position, if access is defined, the value is used.

4   table: you can declare table properties (name, schema, catalog), if none is defined, the java annotation is used.
You can define one or several unique constraints as seen in the example

5   secondary-table: defines a secondary table very much like a regular table except that you can define the primary key / foreign key column(s) through the primary-key-join-column element. On non metadata complete, annotation secondary tables are used only if there is no secondary-table definition, annotations are ignored otherwise.
6   id-class: defines the id class in a similar way @IdClass does
7   inheritance: defines the inheritance strategy (JOINED, TABLE_PER_CLASS, SINGLE_TABLE), Available only at the root entity level
8   sequence-generator: defines a sequence generator
9   table-generator: defines a table generator
10  primary-key-join-column: defines the primary key join column for sub entities when JOINED inheritance strategy is used

不,您无法通过orm.xml或persistence.xml文件执行此操作。

也就是说,您可以通过在每个持久性对象的@Table批注中指定非标准架构来执行类似的操作。

@Entity
@Table(name = "PERSON", schema="SOME_OTHER_SCHEMA")
public class Person {
    . . . 
}

暂无
暂无

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

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