-->
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.  [ 5 posts ] 
Author Message
 Post subject: executing Stored Procedures from Hibernate
PostPosted: Sun Jul 18, 2010 1:23 am 
Newbie

Joined: Sun Jul 18, 2010 1:14 am
Posts: 2
Hi All,

Im new to hibernate. Please let me know how to call a stored procedure which stored in a database. I use hibernate with annotations and have no hbm file. Can i call my sp using a named query?. How can i call my SP to get a resultset?

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: executing Stored Procedures from Hibernate
PostPosted: Sun Jul 18, 2010 5:36 am 
Newbie

Joined: Thu Jul 15, 2010 6:44 am
Posts: 13
A simple search would have given you the answer, however here you go http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querysql.html

_________________
Vikram.
Http://enterprisejava4u.blogspot.com


Top
 Profile  
 
 Post subject: Re: executing Stored Procedures from Hibernate
PostPosted: Sun Jul 18, 2010 11:34 pm 
Newbie

Joined: Sun Jul 18, 2010 1:14 am
Posts: 2
I have already gone thru that tutorial. . I wanted to call an existing SP which is already stored in the database(MySQL) not to create anything. Is it possible?.I tried it in the following way. But it gave me an error saying " 13:16:56,292 ERROR PARSER:56 - line 1:1: unexpected token: call
Initial SessionFactory creation failed.java.lang.IllegalArgumentException: node to traverse cannot be null!" in the log.

Here are my codes:
Code for the entity-----

Code:
@Entity
@Table(name="tbl_customer")
@NamedQuery( name = "getCustomer",query = "call getCustomer_sp(:cid)")
public class Customer {

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name="id")
   private int id;
   
   @Column(name="first_name")
   private String firstName;
   
   @Column(name="last_name")
   private String lastName;
   
   @OneToOne(cascade=CascadeType.ALL)
   //@Column(name="address_id")
   private Address address;     
     
   @OneToMany(mappedBy="customer",targetEntity=Order.class,cascade=CascadeType.ALL)
   private List<Order> orders = new ArrayList<Order>();
   

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   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;
   }

   public Address getAddress() {
      return address;
   }

   public void setAddress(Address address) {
      this.address = address;
   }

   public List<Order> getOrders() {
      return orders;
   }

   public void setOrders(List<Order> orders) {
      this.orders = orders;
   }
   
   @Override
   public String toString() {
      return firstName + " " + lastName;
   }
}



My CustomerDAOImpl class where i want to use the SP

Code:
public class CustomerDAOImpl implements CustomerDAO {

   @Override
   public Customer getCustomer(int id) {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      return (Customer)session.getNamedQuery("getCustomer").setParameter(0, id).uniqueResult();
   }

}



I have configured my Customer Entity class in hibernateconfig.xml


Everything worked properly until i insert the @NamedQuery part to my Customer class. Where did I go wrong?


Please help me :(


Top
 Profile  
 
 Post subject: Re: executing Stored Procedures from Hibernate
PostPosted: Mon Jul 19, 2010 1:26 am 
Newbie

Joined: Thu Jul 15, 2010 6:44 am
Posts: 13
I think you are missing the parenthessis...

"{ ? = call getCustomer_sp(:cid)") }

the devil is in the details...

Also you would have to map the entity property with columns in the cursor returned by your sp.

something on this lines.

Code:

<sql-query name="selectAllEmployees_SP" callable="true">
    <return alias="emp" class="Employment">
        <return-property name="employee" column="EMPLOYEE"/>
        <return-property name="employer" column="EMPLOYER"/>
        <return-property name="startDate" column="STARTDATE"/>
        <return-property name="endDate" column="ENDDATE"/>
        <return-property name="regionCode" column="REGIONCODE"/>
        <return-property name="id" column="EID"/>
        <return-property name="salary">
            <return-column name="VALUE"/>
            <return-column name="CURRENCY"/>
        </return-property>
    </return>
    { ? = call selectAllEmployments() }
</sql-query>


_________________
Vikram.
Http://enterprisejava4u.blogspot.com


Top
 Profile  
 
 Post subject: Re: executing Stored Procedures from Hibernate
PostPosted: Thu Oct 04, 2012 1:54 am 
Newbie

Joined: Fri Sep 28, 2012 2:02 am
Posts: 2
hi hibervish r u got solution please upload it because am also newbie to hibernate

thanks & regards
Leo


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.