-->
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: using @Loader to set NativeQuery params
PostPosted: Sat Jan 27, 2007 6:08 pm 
Newbie

Joined: Thu Nov 02, 2006 11:06 am
Posts: 3
Hello,

I am wondering if it's currently possible to set the NativeQuery parameters when using the @Loader annotation to load a @ManyToMany collection(or any relationship).

The problem is that I have an object which has a lazy loaded ManyToMany relationship that is loaded by the NativeQuery which accepts several parameters say, :one , :two. I have to use specific names for the query parameters and not the question marks (?). Every time I access that propery I get a org.hibernate.QueryException: Not all named parameters have been set: [one] . When I use a question mark instead of the explicit parameter name it's trying to set its value with the ID value of the object.

I spent a lot of time finding this on the forum, ducumentation, source code but, I couldn't find anything that helped.

Thanks a lot for your help!


Code sample:
@Entity
@NamedNativeQuery(name="theQuery",
query="SELECT a, b FROM zTbl WHERE zTbl.num = :one",
resultSetMapping = "mapToNums")
@SqlResultSetMapping(name="mapToNums", entities=@EntityResult(entityClass=Nums.class)

public class Try{
@Id private String id;
private Integer one;

@ManyToMany(fetch=FetchType.LAZY)
@Loader(namedQuery = "theQuery")
Set<Nums> dbViewReports = new HashSet<Nums>();

...
...
}


Full stack trace of any exception that occurs:

org.hibernate.QueryException: Not all named parameters have been set: [one] [SELECT a, b FROM zTbl WHERE zTbl.num = :one]
at org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:291)
at org.hibernate.impl.SQLQueryImpl.verifyParameters(SQLQueryImpl.java:199)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:143)
at org.hibernate.persister.collection.NamedQueryCollectionInitializer.initialize(NamedQueryCollectionInitializer.java:54)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:309)
at java.util.Formatter$FormatSpecifier.printString(Formatter.java:2790)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2673)
at java.util.Formatter.format(Formatter.java:2430)
at java.io.PrintStream.format(PrintStream.java:899)
at java.io.PrintStream.printf(PrintStream.java:800)
at com.citi.gpb.portal.model.dashboard.TEMP.TempMarketValueIntegrationTest.testAccountValueViewTopAc(TempMarketValueIntegrationTest.java:26)
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 junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 5:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Quote:
I have to use specific names for the query parameters and not the question marks (?)

Why?

This query have to take the id(s) as parameter and that's it, because the loader is supposed to query the collection by id.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 7:24 pm 
Newbie

Joined: Thu Nov 02, 2006 11:06 am
Posts: 3
Thank you for your reply Emmanuel.

You are right it's joined by the primary key but, I have a situation where I need to use the values of these two parameters say :one, :two in multiple places in the query. If I use question marks (?) it treats every question mark as a new parameter.

But, aside of that when I switch to question marks (?) it does the job executes the query, populates the object however throws a null pointer Excpetion in PersistenSet.iterator. On the other hand, when I directly call the query without the loader it works just fine.

That's the excpetion I get:

java.lang.NullPointerException
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:164)



Thanks in advance for the reply.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 03, 2007 8:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
there is probably some previous warns or errors in your logs, the NPE is strange (unless you continue to use a session that has already raised an exception)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 05, 2007 12:49 pm 
Newbie

Joined: Thu Nov 02, 2006 11:06 am
Posts: 3
It looks like currently the only solution for me is to have a hibernate session in the getter method of that collection and to explicitly call the query and set its named parameters. Which I know is a bad thing to do but, I don't see any other "solution." It's impossible for me to decouple this collection from the object. Or may be I should create a custom loader...

Thanks for your help Emmanuel


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.