JPA - EclipseLink - 如何更改默认架构 [英] JPA - EclipseLink - How to change default schema

查看:129
本文介绍了JPA - EclipseLink - 如何更改默认架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用weblogic和oracle编写Web应用程序。
数据源是通过JNDI配置的,受限制的数据库用户可以DML到表中,但不能使用DDL。正如您可能猜到的那样,该用户不是这些表的所有者,但是他被授予了访问权限。

I'm programming a web application using weblogic and oracle. the datasource is configured through JNDI, with a restricted database user who can DML into tables, but can't DDL. As you may guess, that user isn't the owner of those tables, but he's granted access.

假设他是GUEST_USER

Let's say he is GUEST_USER

应用程序正在使用JPA + EclipseLink,并且已经定义了许多实体。我不想在每个实体类中写入更改模式的属性。
我已尝试使用此代码的SessionCustomizer。

The application is using JPA + EclipseLink, and have lots of entities already defined. I don't want to write in each an every entity class the attribute to change schema. I've tried a SessionCustomizer, with this code.

public class MyCustomizer implements SessionCustomizer{

    @Override
    public void customize(Session session) throws Exception {

    session.executeNonSelectingSQL("ALTER SESSION SET CURRENT_SCHEMA = OWNERS_SCHEMA");
    }
}

似乎有一些未初始化的东西,我得到了一个空指针异常,我甚至不确定这是否是在使用它们之前更改连接的模式的方法。
任何样品或想法?

It seems that there's something uninitiallized, I'm getting a null pointer exception, I'm not even sure if this is the way to change the schema for the connections before they are used. Any samples or ideas?

预先感谢您的帮助!

推荐答案

如果所有实体都使用相同的模式,则可以使用xml映射文件来定义默认模式。

If all of the entities use the same schema you can use an xml mapping file to define a default schema.

这样的事情应该有效(例如JPA 2.0,更改schemaLocation为1.0)

Something like this should work (example is for JPA 2.0, change the schemaLocation for 1.0)

orm。 xml:

<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>OWNERS_SCHEMA</schema>
        </persistence-unit-defaults>
    </persistence-unit-metadata>   
    . . .
</entity-mappings>

persistence.xml:

persistence.xml:

<persistence
    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"
    version="2.0" >
    <persistence-unit name="foo">
        . . .
        <mapping-file>orm.xml</mapping-file>
        . . .
    </persistence-unit>
</persistence>

这篇关于JPA - EclipseLink - 如何更改默认架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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