【问题标题】:About hibernate NamedNativeQuery关于hibernate NamedNativeQuery
【发布时间】:2009-09-15 15:37:35
【问题描述】:

我是 hibernate 的新手,我正在使用 @javax.persistence.NamedNativeQuery 将存储的 proc 调用从 hibernate 解析到 mysql,但我遇到了错误。

请帮忙:

我的持久类是:

@Entity    
@javax.persistence.NamedNativeQuery(name = "SampleNameQuery", query = "call spS_NamedQuery(?,?)", resultClass = NamedQuery.class)    
public class NamedQuery {

 @Id
 public String name;

 @Column
 public String value;
}

我的mysql存储过程是:

DELIMITER $$

DROP PROCEDURE IF EXISTS `cpgDB`.`spS_NamedQuery`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `spS_NamedQuery`(IN name VARCHAR(255),OUT var_value VARCHAR(255))
BEGIN

SET var_value = (SELECT value FROM NamedQuery WHERE NamedQuery.name = name);
END$$

DELIMITER ;

调用这段代码的主要方法如下:

public static void main(String[] args) throws Exception {
  Transaction trx = null;
  Session session = HibernateSessionFactory.getSession();
  try {
   trx = session.beginTransaction();

   org.hibernate.Query query = session.getNamedQuery("SampleNameQuery");
   query.setParameter(0,"fsdfsdf");       
   String value = "";
   query.setParameter(1,value);       
   List objList = query.list();    
   trx.commit();
  } catch (Exception ex) {
   trx.rollback();
   throw ex;
  } finally {
   HibernateSessionFactory.closeSession();
  }    
 }

我的休眠配置文件如下:

<?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="connection.username">xxxx</property>
  <property name="connection.url">jdbc:mysql://127.0.0.1:3306/cpgDB</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="myeclipse.connection.profile">MySQL</property>
  <property name="connection.password">xxxxx</property>
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hbm2ddl.auto">update</property>
  <property name="show_sql">true</property>
  <property name="format_sql">true</property>  
  <mapping class="Demo.NamedQuery"/>
 </session-factory>
</hibernate-configuration>

在执行代码时,我收到以下错误/异常:

Sep 15, 2009 8:54:16 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: OUT or INOUT argument 2 for routine cpgDB.spS_NamedQuery is not a variable or NEW pseudo-variable in BEFORE trigger
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
 at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
 at org.hibernate.loader.Loader.doList(Loader.java:2214)
 at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
 at org.hibernate.loader.Loader.list(Loader.java:2090)
 at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
 at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
 at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
 at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
 at Demo.TestDrive.main(TestDrive.java:44)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: OUT or INOUT argument 2 for routine cpgDB.spS_NamedQuery is not a variable or NEW pseudo-variable in BEFORE trigger
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:930)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2864)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1567)
 at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1154)
 at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:679)
 at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1256)
 at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
 at org.hibernate.loader.Loader.getResultSet(Loader.java:1778)
 at org.hibernate.loader.Loader.doQuery(Loader.java:662)
 at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
 at org.hibernate.loader.Loader.doList(Loader.java:2211)
 ... 7 more

请帮助我解决问题并帮助我纠正它。也请向我推荐合适的链接,在那里我可以了解有关此技术的更多信息。

提前致谢

阿什

【问题讨论】:

    标签: mysql hibernate stored-procedures


    【解决方案1】:

    我无法找到这个问题的答案,我当时实施的解决方案是在我的存储过程中不使用“out”字段,只通过 select 命令返回 sql 查询的结果。

    可能 Hibernate 没有完全实现这个返回 'Out' 参数的功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-06
      • 2015-09-01
      • 1970-01-01
      • 2021-10-12
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      相关资源
      最近更新 更多