ORA-00942 Hibernate SQLException(无法找到表)
我正在尝试运行一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
也许您需要指定架构或类似的内容:
同时确保 Hibernate 使用的帐户 (
SYSTEM
) 有权访问该表。Perhaps you need to specify the schema, or something like that:
Also make sure that account used by Hibernate (
SYSTEM
) has rights to access that table.我遇到了同样的问题,经过很长时间的尝试,我发现了
它,因为该应用程序无法访问存储在我的数据库中名为“system”的模式下的表。通过在我的
Hibernate.cfg.xml 文件
中添加以下行解决了该问题:这是添加的行,用于提供对我创建的表的绝对路径/访问权限。这是完整的 hibernate.cfg.xml 配置:
我希望有一天这会对某人有所帮助。
I ran into the same issue and after long time of trying to find out I was getting
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 myHibernate.cfg.xml file
: here is the line added to give absolute path/access to the table I created .Here is the full
hibernate.cfg.xml
configuration :I hope this will help someone someday.
就我而言,我与 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.
看起来您可能缺少 '@Table'注解。
您可以像这样添加它:
Looks like you might be missing the '@Table' annotation.
You can add it like so:
也许您选择了错误的数据库:
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"
我的问题:就我而言,我有两个数据库(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";
此异常意味着您的表尚未创建,并且您正在对其执行查询。因此,要解决此异常,您必须首先创建表...
将此行添加到您的配置文件中。这里 Hibernate 会自动创建你的表。
但如果你想进一步更新你的表,请使用它,
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.
But if you want to update your table further use this instead,
在 hibernate 配置文件中使用
create。瞧..!!
use
<property name="javax.persistence.schema-generation.database.action">
create</property>
in your hibernate config file. voila..!!由于外键或同义词而映射的不同表(可能不存在或授予权限不适用于该表)可能会出现问题。
对我来说,问题在于该表中的一列与另一个架构表进行了映射,并且缺少 .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.
在我的例子中,表的同义词不存在,并且在代码中我们直接使用表名,在这种情况下,同义词应该存在,或者在代码中使用像 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
如果您使用 Oracle DB,还请检查连接用户对该表所用 SEQUENCE 的 SELECT 权限。发生同样的错误,并且可能会产生误导。
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.