首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将混合模式设置为orm/sistence.xml中持久性单元中的某些类

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

Stack Overflow用户
提问于 2016-05-16 20:23:16
回答 1查看 2.4K关注 0票数 0

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

我知道可以使用java注释,但是这是一个需要最终用户相应配置的产品(因此我希望能够在xml文件中实现这一点)。

目前,我发现了这样一个例子:

orm.xml

代码语言:javascript
复制
  <?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

代码语言:javascript
复制
<?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映射文件的持久性单元上设置了模式“安全性”(使其成为注释可以更改的缺省值?)。但是这使得模式对于persistence.xml中列出的两个类都是隐式的“安全性”。

与上面所需的相反:

  • 一种高效地在持久化单元中的类子集上列出模式的方法(除非唯一的方法是在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

以下是最相关的节选:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2016-05-16 20:35:38

不,您不能通过orm.xml或persistence.xml文件这样做。

也就是说,您可以通过在每个单个持久性对象的@Table注释中指定非标准模式来做类似的事情。

代码语言:javascript
复制
@Entity
@Table(name = "PERSON", schema="SOME_OTHER_SCHEMA")
public class Person {
    . . . 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37262634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档