开发者

ORA-00942 SQLException with Hibernate (Unable to find a table)

开发者 https://www.devze.com 2023-02-26 14:12 出处:网络
I\'m trying to run a simple Hibernate application, but I get this error: org.hibernate.exception.SQLGrammarException: could not execute query

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开发者_StackOverflow中文版 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


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.


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.


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


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

You can add it like so:

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


Maybe you have chosen the wrong database:

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

CREATE TABLE "HR"."IDT"


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";


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>


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


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.


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


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..


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号