Oracle表被Hibernate删除了;从回收站闪回DB。获取错误“SQL错误:942,SQLState:42000”

时间:2017-09-22 08:49:39

标签: java spring oracle hibernate jpa

使用Spring Data + JPA + Hibernate + Maven项目。

事件顺序:

  1. 我有一个使用上面的工作代码,用于使用Favorites查询JPARespository表。我在JPA属性中添加了一行props.put("hibernate.hbm2ddl.auto");,并删除了收藏夹表。纯粹的恐慌!

    properties.put( “hibernate.hbm2ddl.auto”,environment.getRequiredProperty( “hibernate.hbm2ddl.auto”));

  2. 我评论导致问题的代码。我请求DBA重新创建表,她说它在回收站中并且“闪回”它。

    private Map jpaProperties() { Map<String,Object> properties = new HashMap<String,Object>(); properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect")); // properties.put("hibernate.hbm2ddl.auto", // environment.getRequiredProperty("hibernate.hbm2ddl.auto")); properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql")); properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql")); return properties; }

  3. 数据库配置方法:

    `@Primary
     @Bean(name = "dataSource")
     public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(environment
                .getRequiredProperty("rg.jdbc.driverClassName"));
    dataSource.setUrl(environment.getRequiredProperty("rg.jdbc.url"));
    dataSource.setUsername(environment
                .getRequiredProperty("rg.jdbc.username"));
    dataSource.setPassword(environment
                .getRequiredProperty("rg.jdbc.password"));
    return dataSource;
    }`
    
    1. 我可以使用代码中使用的相同用户凭据在SQL Developer中看到表。但是,如果我使用JPARepository
    2. 使用findAll()扩展类查询数据库

      我收到以下错误。我可以在数据库中看到表,但不能通过Java代码看到。

      有人可以帮我理解上面的属性做了什么以及为什么我不能通过代码看到表,但可以在SQL Developer中看到它吗?另外,我该如何解决这个问题?

      Hibernate: 
      select
          favorite0_.FAVORITE_ID as FAVORITE_ID1_1_,
          favorite0_.category as category2_1_,
          favorite0_.created as created3_1_,
          favorite0_.CREATED_BY as CREATED_BY4_1_,
          favorite0_.description as description5_1_,
          favorite0_.name as name6_1_,
          favorite0_.OBJECT_TYPE_ID as OBJECT_TYPE_ID7_1_ 
      from
          FAVORITES favorite0_
      2017-09-22 02:21:20 [http-bio-8080-exec-2] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 942, SQLState: 42000
      2017-09-22 02:21:20 [http-bio-8080-exec-2] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ORA-00942: table or view does not exist
      
      Sep 22, 2017 1:24:11 AM org.apache.catalina.core.StandardWrapperValve invoke
      SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/RG-NG] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
      java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
      
      at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
      at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
      at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1017)
      at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:655)
      at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:249)
      at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:566)
      at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:215)
      at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:58)
      at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:776)
      at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:897)
      at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1034)
      at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3820)
      at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3867)
      at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1502)
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
      at org.hibernate.loader.Loader.getResultSet(Loader.java:2117)
      at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1900)
      

2 个答案:

答案 0 :(得分:0)

我的猜测是你以前拥有表格权限,因为它已被删除你的权限已经消失,请你的DBA恢复桌面上的权限。

答案 1 :(得分:0)

感谢@CyrilleMODIANO和@Zeromus指出我正确的方向。我解决了这个问题。

有两个问题:

1)我开启了架构验证。

properties.put("hibernate.hbm2ddl.auto", SchemaAutoTooling.VALIDATE.name().toLowerCase());

这告诉我其中一个实体类存在问题,并且对于使用@Id注释的变量没有此注释

@GeneratedValue(strategy = GenerationType.AUTO)

2)数据源注入搞砸了。 DB1的数据源对象被注入到DB2的PU中。

添加限定符并为其指定唯一名称。

    @Primary
    @Bean(name = "rgDataSource")
    public DataSource rgDataSource()

    @Primary
    @Bean(name = "rgEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        EntityManagerFactoryBuilder builder,
        @Qualifier("rgDataSource") DataSource dataSource) {