Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Stored procedures JPA and mappings
PostPosted: Tue Dec 15, 2009 6:13 am 
Newbie

Joined: Tue Dec 15, 2009 5:50 am
Posts: 2
Hi all,

I have a question regarding the stored procedures in JPA. I have the following Entity called RenGaraza which uses two stored procedures "vrniGarazo" and "vrniGarazoPk" that do basically the same thing. The only difference is in the were part of the procedure - the resultSet is the same in both cases.

Code:
@SuppressWarnings("serial")
@Entity
@Table(name = "REN_GARAZA")
@Loader(namedQuery = "vrniGarazoPk" )
@NamedNativeQueries({
      @NamedNativeQuery(name = "vrniGarazoPk", query = "{ call API_PVN_DST.vrni_dst_park_pk(?, :vGarId)}", resultClass = RenGaraza.class, hints = { @javax.persistence.QueryHint(name = "org.hibernate.callable", value = "true") }),
      @NamedNativeQuery(name = "vrniGarazo", query = "{ call API_PVN_DST.vrni_dst_park_fk(?, :vDstSid)}", resultClass = RenGaraza.class, hints = { @javax.persistence.QueryHint(name = "org.hibernate.callable", value = "true") })
      })
public class RenGaraza implements java.io.Serializable {
   ...
}


The stuff works just fine when I call the procedure directly using the first named query - "vrniGarazo":

Code:
public void testGaraza_dstsid() {

      Query qGaraza = session.getNamedQuery("vrniGarazo");
      qGaraza.setParameter("vDstSid", v_dst_sid);
      List<RenGaraza> listGaraza = qGaraza.list();
      listGaraza.size();
      System.out.println("Garaza, st. zapisov: " + listGaraza.size());
      for (RenGaraza garaza : listGaraza) {
         int garId = garaza.getGarId();
         System.out.println(" gar_id: " + garId);
         assertNotNull(garId);
   }
}


However, I need to call the same entity via another Entity "RenDelistavb":

Code:
@Entity
@Table(name = "REN_DELISTAVB")
@Loader(namedQuery = "vrniDelStavbe")
@NamedNativeQueries( {
      @NamedNativeQuery(name = "vrniDelStavbe", query = "{ call API_PVN_DST.vrni_dst_dst_pk(?, :vDstSid)}", resultClass = RenDelistavb.class, hints = { @javax.persistence.QueryHint(name = "org.hibernate.callable", value = "true") }),
      @NamedNativeQuery(name = "vrniGarazoDstSid", query = "{ call API_PVN_DST.vrni_dst_park_fk(?, :vDstSid)}", resultClass = RenGaraza.class, hints = { @javax.persistence.QueryHint(name = "org.hibernate.callable", value = "true") }) })
public class RenDelistavb implements java.io.Serializable {
...
private Set<RenGaraza> renGarazas = new HashSet<RenGaraza>(0);

@Loader(namedQuery = "vrniGarazoDstSid")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "renDelistavb")
public Set<RenGaraza> getRenGarazas() {
   return this.renGarazas;
}
...
}


I have tried all kinds of mambo-jumbo like adding the ResultSetMapping to the procedure, adding different loader and so on, but could not make it to work.

I get the NullPointerException and if I do some debugging I can see that "com.sun.jdi.InvocationException occurred invoking method." error. I additionally debuged the procedure that is called in the second case and it sims that it executes correcly even using the correct id from the RenDelistavb and that it actually generates the resultSet, but it is not returned correctly.

I am using the latest version of hibernate (3.4.0.GA)

Now, obviously my question is what am I doing wrong and if it is even possible to call the original clall (RenGaraza ) in the manner I am trying to do it?
If the answer is not, is there any workaround or another solution?

Thanks for the answers.

Regards,
Matej


Top
 Profile  
 
 Post subject: Re: Stored procedures JPA and mappings
PostPosted: Wed Dec 16, 2009 5:35 am 
Newbie

Joined: Tue Dec 15, 2009 5:50 am
Posts: 2
The followup:

Yesterday I intensively debugged the hibernate code and found out that the Set<RenGaraza> is initialized correctly but there is an obvious bug in the procedure since the Set set in the PersistentSet class is not initialized anywhere and as a consequence

Code:
public Iterator iterator() {
      read();
      return new IteratorProxy( set.iterator() );
}


produces an error:

Code:
java.lang.NullPointerException
   at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:187)
   at com.sinergise.pvnTest.server.procedure.TestApiPvnDst_direkt.testDeliStavb_pk(TestApiPvnDst_direkt.java:89)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.