-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: stored procedure problem with hibernate
PostPosted: Wed Mar 26, 2008 4:24 am 
Newbie

Joined: Wed Mar 26, 2008 3:16 am
Posts: 1
Hi all,
I am using MSSQL and hibernate3.jar and using this POJO

package org.ultimania.model;

public class Employee {

public String first_name;
public String last_name;
public String emp_id;

public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}

public String getEmp_id() {
return emp_id;
}
public void setEmp_id(String emp_id) {
this.emp_id = emp_id;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}


}


my storage procedure is;;

CREATE PROCEDURE GetEmployeeInfo
AS
BEGIN
SELECT EMP_ID,FIRST_NAME,LAST_NAME
FROM EMPLOYEE
END
GO

and using the following hbm file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>

<sql-query name="getEmployeeResults" callable="true">
<return alias="emp" class="org.ultimania.model.Employee">
<return-property name="emp_id" column="EMP_ID" />
<return-property name="first_name" column="FIRST_NAME" />
<return-property name="last_name" column="LAST_NAME" />
</return>
{ ? = call GetEmployeeInfo()}
</sql-query>

</hibernate-mapping>


and if i call this like below from my handler class

[
Session sessionObj = (Session)new Configuration().configure().buildSessionFactory().openSession();

Query query =sessionObj.getNamedQuery("getEmployeeResults");

Employee emp = (Employee)query.uniqueResult();
]


the following error is given::
Exception in thread "main" org.hibernate.HibernateException: Errors in named queries: getEmployeeResults
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:365)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)




but if i use below hbm file instead of previous it works fine

<hibernate-mapping>

<sql-query name="getEmployeeResults" callable="true">
<return-scalar column="emp_id" type="string"/>
<return-scalar column="first_name" type="string"/>
<return-scalar column="last_name" type="string"/>
{call GetEmployeeInfo() }
</sql-query>

</hibernate-mapping>

my requirement is to use return-property rather than return-scalar

plzzzz help....






Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html



Top
 Profile  
 
 Post subject: Exception while working with BFile usertype.
PostPosted: Mon Jul 07, 2008 9:06 am 
Beginner
Beginner

Joined: Wed Dec 06, 2006 6:24 am
Posts: 24
Location: Bangalore
Hi,

as per the above mentioned post, I've tried making use of BFile user type.


Code:


POJO making use of BFileType: TestBfile.java


import java.io.Serializable;


@SuppressWarnings("serial")
public class TestBfile implements Serializable {

   private String evtDocId;
   private String fileName;
   private byte[] filePath;
   private long version;
   /**
    * @return the evtDocId
    */
   public String getEvtDocId() {
      return evtDocId;
   }
   /**
    * @param evtDocId the evtDocId to set
    */
   public void setEvtDocId(String evtDocId) {
      this.evtDocId = evtDocId;
   }
   /**
    * @return the fileName
    */
   public String getFileName() {
      return fileName;
   }
   /**
    * @param fileName the fileName to set
    */
   public void setFileName(String fileName) {
      this.fileName = fileName;
   }
   /**
    * @return the filePath
    */
   public byte[] getFilePath() {
      return filePath;
   }
   /**
    * @param filePath the filePath to set
    */
   public void setFilePath(byte[] filePath) {
      this.filePath = filePath;
   }
   /**
    * @return the version
    */
   public long getVersion() {
      return version;
   }
   /**
    * @param version the version to set
    */
   public void setVersion(long version) {
      this.version = version;
   }
   
   
}

mapping file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 27, 2008 11:49:03 AM by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
    <class name="com.gpj.gl.pa.domain.TestBfile" table="Test_DOCUMENTS_TX" schema="GL_PA">
        <id name="evtDocId" type="string">
            <column name="EVT_DOC_ID" length="15" />

            <generator class="sequence">
               <param name="sequence">
                  PAParticipation
               </param>
            </generator>
        </id>

        <version name="version" type="long">
            <column name="VERSION" precision="15" scale="0" not-null="true" />
        </version>

        <property name="fileName" type="string">
            <column name="FILE_NAME" length="50" not-null="true" />
        </property>
       

        <property name="filePath" type="com.util.BFileType">
            <column name="FILE_PATH" not-null="true" />
        </property>
       
    </class>
</hibernate-mapping>

BFileType class:

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

import oracle.jdbc.driver.OracleConnection;
import oracle.jdbc.driver.OracleTypes;
import oracle.sql.BFILE;



@SuppressWarnings("unchecked")
public final class BFileType implements UserType {

   private static final int BFILE_TYPE = OracleTypes.BFILE;   
   private static final Class RETURNED_CLASS = byte[].class;;
       
   public int[] sqlTypes() {
      return new int[]{BFILE_TYPE};
   }

   public Class returnedClass() {
      return RETURNED_CLASS;
   }

   public boolean equals(Object x, Object y) throws HibernateException {
       
      if (x == y){
         return true;
      }
      if (x == null || y == null){
         return false;
      }
      return x.equals(y);
   }

   public boolean isMutable() {
      return false;
   }
   public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
         throws HibernateException, SQLException {
      BFILE file = (BFILE) rs.getObject(names[0]);
      if (file == null)
         return null;     
      return file.getBytes();
   }
   
   public void nullSafeSet(PreparedStatement st, Object value, int index)
         throws HibernateException, SQLException {
      if (value == null)
         st.setObject(index,null);
      else {     
      
      BFILE file = new BFILE((OracleConnection) st.getConnection(),(byte[])value);       
   
      st.setObject(index, file);  // getting exception here.

      }

   }

   public Object assemble(Serializable arg0, Object arg1)
         throws HibernateException {
      // TODO Auto-generated method stub
      
      return null;
   }
   

   public Object deepCopy(Object arg0) throws HibernateException {
      // TODO Auto-generated method stub
      return arg0;
   }
   
   public Serializable disassemble(Object arg0) throws HibernateException {
      // TODO Auto-generated method stub
      return null;
   }
   
   public int hashCode(Object arg0) throws HibernateException {
      // TODO Auto-generated method stub
      return arg0.hashCode();
   }
   
   public Object replace(Object arg0, Object arg1, Object arg2)
         throws HibernateException {
      // TODO Auto-generated method stub
      return null;
   }
}



I am getting an Exception, in BFileType class at line
st.setObject(index, file);

Exception is:

Hibernate: insert into GL_PA.Test_DOCUMENTS_TX (VERSION, FILE_NAME, FILE_PATH, EVT_DOC_ID) values (?, ?, ?, ?)

2008-07-07 18:21:38,955 ERROR [org.hibernate.jdbc.AbstractBatcher] - <Exception executing batch: >
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at oracle.jdbc.driver.DatumBinder.bind(OraclePreparedStatement.java:15862)
at oracle.jdbc.driver.OraclePreparedStatement.setupBindBuffers(OraclePreparedStatement.java:2866)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10580)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.gpj.gl.pa.test.BFileTest.main(BFileTest.java:39)
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at oracle.jdbc.driver.DatumBinder.bind(OraclePreparedStatement.java:15862)
at oracle.jdbc.driver.OraclePreparedStatement.setupBindBuffers(OraclePreparedStatement.java:2866)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10580)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.gpj.gl.pa.test.BFileTest.main(BFileTest.java:39)

Please provide your suggestions why I am getting this exception.

_________________
Vinay N


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.