简体   繁体   English

Hibernate 表或视图不存在 Oracle

[英]Hibernate Table or view does not exist Oracle

I Have a problem executing an hibernate query on Oracle:我在 Oracle 上执行 hibernate 查询时遇到问题:

my persistence.xml:我的坚持。xml:

<description>ProfileEntityManager</description>
<class>com.entity.User</class>  
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
    <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@XXX:9999/db"/> 
    <property name="javax.persistence.jdbc.user" value="user"/> 
<property name="javax.persistence.jdbc.password" value="password"/> 
    <property name = "hibernate.show_sql" value = "true" />
</properties>

my entity class com.entity.User:我的实体 class com.entity.User:

package com.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name ="USER", schema = "SCHEMA")
public class User{

    @Id
    @Column(name = "USER_ID")
    private String userId;
    @Column(name = "FIRST_NAME")
    private String firstName;
    @Column(name = "LAST_NAME")
    private String lastName;
    
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    
}

my java class:我的 java class:

        //System.out.println(uid);
        List<User> lp = null;
        try {
            lp = em.createNativeQuery("SELECT iu FROM User iu WHERE iu.userId = ?", User.class)
                    .setParameter(1, uid).getResultList();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            writeLog(fw, e.getMessage());
        }

but when i execute my code, i obtain:但是当我执行我的代码时,我得到:

Hibernate: SELECT iu FROM User iu WHERE iu.userId = ?
gen 29, 2021 6:36:59 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 942, SQLState: 42000
gen 29, 2021 6:36:59 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-00942: tabella o vista inesistente

org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Can you help me to find the error?你能帮我找出错误吗?

NOTE: if a use a PreparedStatement with the same connections and query i don't have any issue, so it is not a problem of permissions注意:如果使用具有相同连接和查询的 PreparedStatement 我没有任何问题,所以这不是权限问题

You try to use HQL query with EntityManager.createNativeQuery method.您尝试将HQL查询与EntityManager.createNativeQuery方法一起使用。 This is a mistake.这是个错误。

Try to correct your query in this way:尝试以这种方式更正您的查询:

em.createNativeQuery("SELECT * FROM SCHEMA.USER WHERE USER_ID = :usrid", User.class)
.setParameter("usrid", uid)
.getResultList();

If you use the SCHEMA for all your entities, you can put it to the hibernate config:如果您对所有实体使用SCHEMA ,则可以将其放入 hibernate 配置中:

<property name="hibernate.default_schema" value="SCHEMA"/>

then correct your entity definition in the following way:然后通过以下方式更正您的实体定义:

@Entity
@Table(name ="USER")
public class User {
   // ...
}

and then correct your query:然后更正您的查询:

em.createNativeQuery("SELECT * FROM {h-schema}USER WHERE USER_ID = :usrid", User.class)
.setParameter("usrid", uid)
.getResultList();

PS And by the way, I would suggest you to correct your hibernate dialect definition. PS顺便说一句,我建议您更正您的 hibernate 方言定义。 You should use as specific dialect as possible.您应该尽可能使用特定的方言。 For example if you use Oracle 10g, you should use org.hibernate.dialect.Oracle10gDialect .例如,如果您使用 Oracle 10g,则应使用org.hibernate.dialect.Oracle10gDialect You can still use org.hibernate.dialect.Oracle9iDialect but some new features defined in Oracle10gDialect will not be available.您仍然可以使用org.hibernate.dialect.Oracle9iDialectOracle10gDialect中定义的一些新特性将不可用。

Seems like you need to add this line to your configuration file似乎您需要将此行添加到配置文件中

either任何一个

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

or或者

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM