首页 \ 问答 \ 在Hibernate中调用存储过程(Calling a Stored Procedure in Hibernate)

在Hibernate中调用存储过程(Calling a Stored Procedure in Hibernate)

我刚刚开始学习hibernate,它很有趣。 我使用hibernate将存储过程调用为sql-query时遇到了一些麻烦。 我附上了源码和错误,请帮帮我。 谢谢 :)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Example" table="example">
   <id name="user_id" column="id" >
   <generator class="increment"/>
  </id>
  <property name="user_data">
    <column name="data"/>
  </property>
 </class>

<!-- sql-query must be after class --> 

<sql-query name="getRecordsSP" callable="true" >
<return class="Example">
<return-property name="user_id" column="user_id"></return-property>
<return-property name="user_data" column="user_data"></return-property>
</return>
{call getRecords}
</sql-query>

</hibernate-mapping>

这是java文件:::

    import java.util.Iterator;
    import java.util.List;

    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;


    public class SelectProc {


        public static void main(String[] args) {

            Session session = null;
            //String query = "select example.id, example.data from Example example";

            try{

                SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
                session = sessionFactory.openSession();

                System.out.println("selecting records");
                Query q = session.getNamedQuery("getRecordsSP");
        //      Query q =  session.createSQLQuery("call getRecords","",Example.class);

                System.out.print("Done");
                List l = q.list();


                for(Iterator it = l.iterator() ;it.hasNext();){
                    Object row [] = (Object [])it.next();
                    System.out.println(row.length);
            //      System.out.println(row[0]);
            //      System.out.println("ID " + row[0]);
            //  System.out.println("Data "+ row[1]);
                }

            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                session.flush();
                session.close();

        }
    }
}

错误是:::

call getRecords}

18:33:41,346  WARN JDBCExceptionReporter:38 - SQL Error: 0, SQLState: S0022
18:33:41,347 ERROR JDBCExceptionReporter:46 - Column 'id0_' not found.
18:33:41,348  WARN JDBCExceptionReporter:38 - SQL Error: 0, SQLState: S0022
18:33:41,349 ERROR JDBCExceptionReporter:46 - Column 'id0_' not found.
18:33:41,349 ERROR JDBCExceptionReporter:38 - Could not execute native SQL query
java.sql.SQLException: Column 'id0_' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
    at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144)
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2813)
    at org.hibernate.type.IntegerType.get(IntegerType.java:18)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:62)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:53)
    at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:541)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:233)
    at org.hibernate.loader.Loader.doQuery(Loader.java:337)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:167)
    at org.hibernate.loader.Loader.doList(Loader.java:1201)
    at org.hibernate.loader.Loader.list(Loader.java:1186)
    at org.hibernate.loader.SQLLoader.list(SQLLoader.java:121)
    at org.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:2212)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:85)
    at SelectProc.main(SelectProc.java:28)
org.hibernate.JDBCException: Could not execute native SQL query
    at org.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:2215)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:85)
    at SelectProc.main(SelectProc.java:28)
Caused by: java.sql.SQLException: Column 'id0_' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
    at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144)
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2813)
    at org.hibernate.type.IntegerType.get(IntegerType.java:18)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:62)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:53)
    at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:541)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:233)
    at org.hibernate.loader.Loader.doQuery(Loader.java:337)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:167)
    at org.hibernate.loader.Loader.doList(Loader.java:1201)
    at org.hibernate.loader.Loader.list(Loader.java:1186)
    at org.hibernate.loader.SQLLoader.list(SQLLoader.java:121)
    at org.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:2212)
    ... 2 more

I just started learning hibernate last night and its rather fun. I am some trouble calling a stored procedure as a sql-query with hibernate. I have attached the source and the error, please help me. Thanks :)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Example" table="example">
   <id name="user_id" column="id" >
   <generator class="increment"/>
  </id>
  <property name="user_data">
    <column name="data"/>
  </property>
 </class>

<!-- sql-query must be after class --> 

<sql-query name="getRecordsSP" callable="true" >
<return class="Example">
<return-property name="user_id" column="user_id"></return-property>
<return-property name="user_data" column="user_data"></return-property>
</return>
{call getRecords}
</sql-query>

</hibernate-mapping>

This is the java file :::

    import java.util.Iterator;
    import java.util.List;

    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;


    public class SelectProc {


        public static void main(String[] args) {

            Session session = null;
            //String query = "select example.id, example.data from Example example";

            try{

                SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
                session = sessionFactory.openSession();

                System.out.println("selecting records");
                Query q = session.getNamedQuery("getRecordsSP");
        //      Query q =  session.createSQLQuery("call getRecords","",Example.class);

                System.out.print("Done");
                List l = q.list();


                for(Iterator it = l.iterator() ;it.hasNext();){
                    Object row [] = (Object [])it.next();
                    System.out.println(row.length);
            //      System.out.println(row[0]);
            //      System.out.println("ID " + row[0]);
            //  System.out.println("Data "+ row[1]);
                }

            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                session.flush();
                session.close();

        }
    }
}

The error is:::

call getRecords}

18:33:41,346  WARN JDBCExceptionReporter:38 - SQL Error: 0, SQLState: S0022
18:33:41,347 ERROR JDBCExceptionReporter:46 - Column 'id0_' not found.
18:33:41,348  WARN JDBCExceptionReporter:38 - SQL Error: 0, SQLState: S0022
18:33:41,349 ERROR JDBCExceptionReporter:46 - Column 'id0_' not found.
18:33:41,349 ERROR JDBCExceptionReporter:38 - Could not execute native SQL query
java.sql.SQLException: Column 'id0_' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
    at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144)
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2813)
    at org.hibernate.type.IntegerType.get(IntegerType.java:18)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:62)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:53)
    at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:541)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:233)
    at org.hibernate.loader.Loader.doQuery(Loader.java:337)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:167)
    at org.hibernate.loader.Loader.doList(Loader.java:1201)
    at org.hibernate.loader.Loader.list(Loader.java:1186)
    at org.hibernate.loader.SQLLoader.list(SQLLoader.java:121)
    at org.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:2212)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:85)
    at SelectProc.main(SelectProc.java:28)
org.hibernate.JDBCException: Could not execute native SQL query
    at org.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:2215)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:85)
    at SelectProc.main(SelectProc.java:28)
Caused by: java.sql.SQLException: Column 'id0_' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
    at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144)
    at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2813)
    at org.hibernate.type.IntegerType.get(IntegerType.java:18)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:62)
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:53)
    at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:541)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:233)
    at org.hibernate.loader.Loader.doQuery(Loader.java:337)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:167)
    at org.hibernate.loader.Loader.doList(Loader.java:1201)
    at org.hibernate.loader.Loader.list(Loader.java:1186)
    at org.hibernate.loader.SQLLoader.list(SQLLoader.java:121)
    at org.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:2212)
    ... 2 more

原文:https://stackoverflow.com/questions/3681045
更新时间:2022-02-13 11:02

最满意答案

您的数据库正在恢复中。

它可能是热备份(很可能),或者在recovery.conf中使用SELECT pg_wal_replay_pause()recovery_target_action = 'pause' recovery.conf (使用SELECT pg_is_wal_replay_paused()检查),或者时间点恢复仍然存在运行。

使用主要或完全恢复。


Your database is in recovery.

Either it is a hot standby (most likely), or recovery was paused with SELECT pg_wal_replay_pause() or recovery_target_action = 'pause' in recovery.conf (check with SELECT pg_is_wal_replay_paused()), or a point-in-time-recovery is still running.

Use the primary or complete recovery.

相关问答

更多
  • ssid_id和ap_id的谓词中非常常见的值可以使Postgres选择较小的索引session_lim_values_idx (仅1个date列)比看似更好的拟合更便宜,但更大的索引session_date_ssid_apid_idx (4列)并过滤其余的。 在你的情况下,大约4%的行有ssid_id=42 AND ap_id=1731 。 这通常不应该保证切换到较小的索引。 但是其他一些因素正在发挥作用,可能会使规模倾斜,主要是成本设置和统计数据 。 细节: 保持PostgreSQL有时选择错误的查询计 ...
  • 虚拟交易ID具有“n / nnnn”格式。 真正的XID只是整数。 虚拟xid的第一部分是每个连接唯一的后端标识符; 第二部分是该连接的后端为其事务分配的临时事务ID。 有关详细信息,请参阅src/include/storage/lock.h的VirtualTransactionId的定义。 这些列似乎对应于virtualxid中的virtualxid和/或transactionid和virtualtransaction列。 查看文档 。 如果我是正确的那么: “TX”是持有或等待锁定的交易的虚拟交易ID。 ...
  • psql :使用我的默认用户名登录 psql -U postgres :以postgres用户身份登录 Sudo似乎不需要我。 我使用Postgres.app作为我的OS X postgres数据库。 它清除了确保安装正常并且数据库服务器正常启动的头痛。 在这里查看: http : //postgresapp.com 编辑:感谢@Erwin Brandstetter纠正我对参数的使用。 psql: Logs me in with my default username psql -U postgres: L ...
  • 设置密码仅提供需要它的身份验证方法的密码。 它不会添加为登录指定密码的要求。 是否需要密码由pg_hba.conf控制。 对peer身份验证模式不需要密码,如果用户的unix用户名与他们尝试连接的postgres用户名相同,则允许用户登录。 如果要进行密码验证,请尝试使用md5 auth。 Setting a password only provides the password for authentication methods that require it. It does not add the ...
  • 在RedHat之类的发行版中,它将位于/var/lib/pgsql/pgstartup.log或者在/etc/init.d/postgresql中的init脚本中查找变量PGLOG In RedHat like distributions it would be in /var/lib/pgsql/pgstartup.log or look for the variable PGLOG in the init script in /etc/init.d/postgresql
  • postgres=# COPY venues TO '/tmp/file.csv'; 应该为你工作 postgres=# COPY venues TO '/tmp/file.csv'; should work for u
  • 交易ID计数器在整个服务器上是全局的。 这部分是因为PostgreSQL具有一些全局表,如pg_database , pg_role等,这些表在所有数据库中都是可见的。 为了完成这些事务,我们需要一个全局事务ID。 除了交易标识符之外,您不应该使用交易ID。 由于事务ID环绕,您甚至不能假定较低的事务ID是旧事务。 您可以使用txid_current_snapshot和txid_visible_in_snapshot (如果您使用的是足够新的PostgreSQL)来txid_visible_in_snaps ...
  • 你有两个错误: createdb是一个操作系统命令 ,它不是一个SQL命令。 在psql中,你需要使用SQL语句,那就是: CREATE DATABASE 。 有关详细信息,请参阅手册: http : //www.postgresql.org/docs/current/static/sql-createdatabase.html 每个SQL语句都需要用一个; 。 因为你没有这样做,你的陈述没有执行,因此你没有得到一个错误。 有关详细信息,请参阅手册: http : //www.postgresql.org/ ...
  • 您的数据库正在恢复中。 它可能是热备份(很可能),或者在recovery.conf中使用SELECT pg_wal_replay_pause()或recovery_target_action = 'pause' recovery.conf (使用SELECT pg_is_wal_replay_paused()检查),或者时间点恢复仍然存在运行。 使用主要或完全恢复。 Your database is in recovery. Either it is a hot standby (most likely), ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。