ORA-00942 Hibernate SQLException(无法找到表)

发布于 2024-11-02 19:23:54 字数 2805 浏览 1 评论 0原文

我正在尝试运行一个简单的 Hibernate 应用程序,但收到此错误:

org.hibernate.exception.SQLGrammarException: could not execute query
java.sql.SQLException: ORA-00942: table or view does not exist

我的实体:

package beans;

import javax.persistence.*;

@Entity
public class Idt {
    @Id
    private int id;
    @Column(name="name")
    private String name;

    public Idt(){
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

我的表在 Hr 用户中称为 IDT。

CREATE TABLE "HR"."IDT"
  (
    "ID"   NUMBER NOT NULL ENABLE,
    "NAME" VARCHAR2(20 BYTE),
    CONSTRAINT "IDT_PK" PRIMARY KEY ("ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "USERS" ENABLE
  )
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE
  (
    INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT
  )
  TABLESPACE "USERS" ;

Hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

            <hibernate-configuration>

                <session-factory>

                    <!-- Database connection settings -->
                    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
                    <property name="connection.url">jdbc:oracle:thin:@localhost:1521/xe</property>
                    <property name="connection.username">SYSTEM</property>
                    <property name="connection.password">19141914</property>

                    <!-- SQL dialect -->
                    <property name="dialect">org.hibernate.dialect.OracleDialect</property>

                    <!-- Echo all executed SQL to stdout -->
                    <property name="show_sql">true</property>

                    <property name="current_session_context_class">thread</property>

                    <mapping class="beans.Idt"/>



                </session-factory>

            </hibernate-configuration>     

我猜想这与实体的映射有关,因为 SQLException:

java.sql.SQLException: ORA-00942: table or view does not exist

I'm trying to run a simple Hibernate application, but I get this error:

org.hibernate.exception.SQLGrammarException: could not execute query
java.sql.SQLException: ORA-00942: table or view does not exist

My Entity:

package beans;

import javax.persistence.*;

@Entity
public class Idt {
    @Id
    private int id;
    @Column(name="name")
    private String name;

    public Idt(){
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

My table is called IDT in the Hr user.

CREATE TABLE "HR"."IDT"
  (
    "ID"   NUMBER NOT NULL ENABLE,
    "NAME" VARCHAR2(20 BYTE),
    CONSTRAINT "IDT_PK" PRIMARY KEY ("ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "USERS" ENABLE
  )
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE
  (
    INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT
  )
  TABLESPACE "USERS" ;

Hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

            <hibernate-configuration>

                <session-factory>

                    <!-- Database connection settings -->
                    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
                    <property name="connection.url">jdbc:oracle:thin:@localhost:1521/xe</property>
                    <property name="connection.username">SYSTEM</property>
                    <property name="connection.password">19141914</property>

                    <!-- SQL dialect -->
                    <property name="dialect">org.hibernate.dialect.OracleDialect</property>

                    <!-- Echo all executed SQL to stdout -->
                    <property name="show_sql">true</property>

                    <property name="current_session_context_class">thread</property>

                    <mapping class="beans.Idt"/>



                </session-factory>

            </hibernate-configuration>     

I guess that it is something with the mapping of the entity because of the SQLException:

java.sql.SQLException: ORA-00942: table or view does not exist

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

初心 2024-11-09 19:23:54

也许您需要指定架构或类似的内容:

@Entity 
@Table(schema = "HR")
public class Idt { ... }

同时确保 Hibernate 使用的帐户 (SYSTEM) 有权访问该表。

Perhaps you need to specify the schema, or something like that:

@Entity 
@Table(schema = "HR")
public class Idt { ... }

Also make sure that account used by Hibernate (SYSTEM) has rights to access that table.

清旖 2024-11-09 19:23:54

我遇到了同样的问题,经过很长时间的尝试,我发现了

java.sql.SQLException: ORA-00942: table or view does not exist

它,因为该应用程序无法访问存储在我的数据库中名为“system”的模式下的表。通过在我的 Hibernate.cfg.xml 文件 中添加以下行解决了该问题:这是添加的行,用于提供对我创建的表的绝对路径/访问权限。

<property name="hibernate.default_schema">system</property> 

这是完整的 hibernate.cfg.xml 配置:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:oracledb</property>
        <property name="hibernate.connection.username">usrname</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.default_schema">system</property>
        <property name="show_sql">true</property>
        <mapping resource="com/mkyong/user/DBUser.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

我希望有一天这会对某人有所帮助。

I ran into the same issue and after long time of trying to find out I was getting

java.sql.SQLException: ORA-00942: table or view does not exist

I found out its because the app did nto have access to the table stored in my database under schema called "system". The problem was solved by adding the following line inside my Hibernate.cfg.xml file : here is the line added to give absolute path/access to the table I created .

<property name="hibernate.default_schema">system</property> 

Here is the full hibernate.cfg.xml configuration :

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:oracledb</property>
        <property name="hibernate.connection.username">usrname</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.default_schema">system</property>
        <property name="show_sql">true</property>
        <mapping resource="com/mkyong/user/DBUser.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

I hope this will help someone someday.

煮茶煮酒煮时光 2024-11-09 19:23:54

就我而言,我与 Hibernate 与用户连接,但没有对我尝试查询的表和架构进行 GRANT。

In my case, I was connected with Hibernate with a user without GRANT on the table and schema what I was trying to query.

一场信仰旅途 2024-11-09 19:23:54

看起来您可能缺少 '@Table'注解。

您可以像这样添加它:

@Entity
@Table(name="idt")
public class Idt {
    ...
}

Looks like you might be missing the '@Table' annotation.

You can add it like so:

@Entity
@Table(name="idt")
public class Idt {
    ...
}
梦境 2024-11-09 19:23:54

也许您选择了错误的数据库:

jdbc:oracle:thin:@localhost:1521/xe

CREATE TABLE "HR"."IDT"

Maybe you have chosen the wrong database:

jdbc:oracle:thin:@localhost:1521/xe

CREATE TABLE "HR"."IDT"

红尘作伴 2024-11-09 19:23:54

我的问题:就我而言,我有两个数据库(DB-1 和 DB-2),当我连接到 DB- 时,我需要插入/修改的表位于 DB-2 中1.
我的解决方案:在 DB-1 中使用 DB-2.table name 创建一个公共同义词。

脚本(在 DB-1 中执行):
为“SYNONYMOUS”创建或替换公共同义词“TABLE”。“TABLE”@“PARAMETRAGE_LINK”;

My Problem : In my case, I had two databases (DB-1 and DB-2) and the table where I needed to insert/modify was in DB-2 whene I'm connected to DB-1.
My solution : Creat a public synonymous in DB-1 with the DB-2.table name.

The script ( executed in DB-1 ) :
CREATE OR REPLACE PUBLIC SYNONYM "TABLE" FOR "SYNONYMOUS "."TABLE"@"PARAMETRAGE_LINK";

攒一口袋星星 2024-11-09 19:23:54

java.sql.SQLException:ORA-00942:表或视图不存在。

此异常意味着您的表尚未创建,并且您正在对其执行查询。因此,要解决此异常,您必须首先创建表...

将此行添加到您的配置文件中。这里 Hibernate 会自动创建你的表。

<property name="hbm2ddl.auto">create</property>

但如果你想进一步更新你的表,请使用它,

<property name="hbm2ddl.auto">update</property>

java.sql.SQLException: ORA-00942: table or view does not exist.

This exception means that your table is not created, and you are performing queries on it. So to resolve this exception you have to create the table first...

Add this line to your configuration file. Here Hibernate will create your table automatically.

<property name="hbm2ddl.auto">create</property>

But if you want to update your table further use this instead,

<property name="hbm2ddl.auto">update</property>
感受沵的脚步 2024-11-09 19:23:54

在 hibernate 配置文件中使用 create 。瞧..!!

use <property name="javax.persistence.schema-generation.database.action">create</property> in your hibernate config file. voila..!!

舟遥客 2024-11-09 19:23:54

由于外键或同义词而映射的不同表(可能不存在或授予权限不适用于该表)可能会出现问题。

对我来说,问题在于该表中的一列与另一个架构表进行了映射,并且缺少 .ex、公共同义词。

Issue could be with different table(might not exists or grant privilege is not for that table) mapped due to foreign key or synonym.

For me the issue was with a column in that table which had mapping with another schema-table, and it was missing.ex, public-synonym.

赴月观长安 2024-11-09 19:23:54

在我的例子中,表的同义词不存在,并且在代码中我们直接使用表名,在这种情况下,同义词应该存在,或者在代码中使用像 select * from DBOwner.tablename 这样的查询

in my case synonym of the table was not there, and in code we were using table name directly, in this case the synonym should be there, or use query like select * from DBOwner.tablename in code

迷乱花海 2024-11-09 19:23:54

如果您使用 Oracle DB,还请检查连接用户对该表所用 SEQUENCESELECT 权限。发生同样的错误,并且可能会产生误导。

If you're working with Oracle DB, also check connection user's SELECT rights on the SEQUENCE used for that table. The same error occurs and it may be misleading..

梦幻的心爱 2024-11-09 19:23:54

如果您的表是新的,则检查该用户的表授权。

if your table is new then check table grants for that user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文