java - 我如何告诉我的数据库某些更改是由 hibernate 而不是其他应用程序进行的?

标签 java spring oracle hibernate

我是 hibernate 的新手,我遇到了这个问题。 我使用 Oracle 数据库和 Hibernate+Maven+Netbeans。我的目的是只能使用我授权的应用程序对我的数据库进行更改。不可能从 SQL 控制台或其他程序进行更改。为此,我在数据库中创建了一个表:

CREATE TABLE DATA(
   name  char(30),
   day   integer,
   month integer,
   year  integer );

像这样的触发器限制对我的数据库的任何访问:

  CREATE OR REPLACE TRIGGER tri_block
     BEFORE INSERT OR UPDATE OR DELETE ON data
    BEGIN
       IF  TO_NUMBER(TO_CHAR(SYSDATE,'hh24')) < 12
           OR TO_NUMBER(TO_CHAR(SYSDATE,'hh24')) >= 12
            OR TO_CHAR(SYSDATE,'dy') in ('sun','sat') THEN 
    RAISE_APPLICATION_ERROR (-20000, 'changes can not be made');
    END IF;
   END;

这是我的 HibernateUtil.java :

 package com.scooby.util;

    import com.scooby.DATA;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure()
                                .addAnnotatedClass(DATA.class).buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

  }

我听说过一些有关 ORACLE 上下文的内容以及有关 hibernate 中 session 的内容。有人可以解释一下吗?

最佳答案

触发器可能不是防止未经授权的写入访问的好解决方案。标准解决方案是为您的应用程序创建一个新用户,然后向所有用户授予/撤销必要的 Oracle 权限,以确保只允许您的应用程序写入某些表。

如果您对这个解决方案不满意,还有sys_context solution ,这绝对比另一个不太安全。虽然它提供了更广泛的审计功能,但也需要在数据库端进行更多工作,以及从应用程序调用存储过程。

关于java - 我如何告诉我的数据库某些更改是由 hibernate 而不是其他应用程序进行的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32097023/

相关文章:

java - 如何在 Swing 应用程序中运行 Javafx 舞台/场景?

java - mvn azure 函数 :deploy creates 'not valid storage account name'

java - 由于未找到 JRE,Eclipse 无法打开

java - 如何从java连接到url

java - 如何通过 Spring Rest Api 检查文件是否已完全下载

spring - Hibernate 二级缓存在运行几个 Spring 测试时关闭

java - Spring条件yaml属性值

oracle - 如何在不使用 DESCRIBE 命令的情况下描述 Oracle 中的表?

sql - Oracle MINUS 两种方式(获得选择之间的差异)

java - 无法在 Oracle 数据源连接中将自动提交值设置为 false