【问题标题】:spring data and ORA-00942: table or view does not existspring 数据和 ORA-00942: 表或视图不存在
【发布时间】:2018-09-15 19:40:04
【问题描述】:

我对 Spring Data 和 Oracle 12 数据库有一些有趣的问题

我明白了

java.sql.SQLSyntaxErrorException:
ORA-00942表或视图没有 存在

但是当我使用 JdbcTemplate 时它可以工作! Spring 数据和 JdbcTemplate 使用相同的数据源。 Liquibase 迁移也可以正常工作

我尝试将模式用于模型,但没有成功。

@Getter
@Setter
@Entity
@Table(name = "tb_accounts", schema = "rx")
public class CustomerAccount {

   @Id
   @Column(name = "id")
   private String id;

   private String accountNo;
}

@Repository
public interface CustomerAccountRepository extends JpaRepository<CustomerAccount, String> {

}

但正如我所说,它适用于 JdbcTemplate

@Repository
public class CustomerAccountDao {

    @Autowired
    private DataSource dataSource;

    private JdbcTemplate jdbcTemplate;

    @PostConstruct
    private void postConstruct() {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public List<CustomerAccount> findAll() {
    return jdbcTemplate.query("select * from tb_accounts", (rs, i) -> {

        CustomerAccount account = new CustomerAccount();
        account.setId(rs.getString("id"));

        return account;
    });
}

迁移文件

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">

    <changeSet id="1" author="user1">
        <createTable tableName="tb_accounts">
            <column name="id" type="VARCHAR(256)">
                <constraints primaryKey="true"/>
            </column>
            <column name="accountNo" type="VARCHAR(256)"/>

        </createTable>

    </changeSet>

</databaseChangeLog>

有什么想法吗?我应该检查什么?我花了几个小时寻找原因,但没有任何帮助:(

【问题讨论】:

  • 您在 @Table 注释中声明了架构,但您没有在本机查询中指定任何架构。
  • 原生查询在有和没有架构的情况下都可以工作。 Spring Data 的问题。我尝试过使用和不使用模式,但例外情况相同。我想它说明了错误的 Spring Data 查询,但为什么......
  • 我猜 JDBC_Template 指的是存在的 TB_ACCOUNTS 表,而 Spring 数据试图找到不存在的 tb_accounts 表。 Oracle中的表名区分大小写,TB_ACCOUNTS &lt;&gt; tb_accounts
  • 年,你是对的!我没有注意到你的消息。 3 分钟前找到原因...

标签: spring oracle spring-data


【解决方案1】:

它需要使用 TB_ACCOUNTS 而不是 tb_accounts...

【讨论】:

  • 它需要在哪里使用?为什么?
  • 该死。我有同样的问题,这个答案没有解决它。
【解决方案2】:
  1. 1)如果你已经有一个现成的,你将使用表格注释 数据库中的表,否则,如果数据库中没有表 具有您在注释中声明的名称的数据库,您将 遇到以下错误。
  2. 这个错误不是很大的错误,如果你在你的项目中将 spring.jpa.hibernate.ddl-auto 的值设置为 none 并运行你的项目 2 次,你就不会再出现这个错误了。
  3. 例如,我在 Spring boot 中创建了一个项目,并在其中创建了一个 Entity 类,并通过在其上添加 @Table 注释,我编写了一个尚未在我的数据库中的表名。当我运行该项目时,您将收到您遇到的错误。
  4. 4.如果我随后阻止它在 application.properties 部分的数据库中重新创建表并再次运行项目,我将 不会遇到此错误。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2016-09-17
  • 2019-01-22
  • 2015-11-09
  • 2011-12-10
  • 2015-01-30
  • 2016-08-23
  • 2020-10-16
  • 2018-02-05
相关资源
最近更新 更多