-->
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.  [ 9 posts ] 
Author Message
 Post subject: Dynamic instantion + Java dynamic arguments
PostPosted: Mon Mar 10, 2008 9:16 am 
Newbie

Joined: Mon Mar 10, 2008 8:55 am
Posts: 9
Hi,

I'd like to run a hql query with dynamic instantiation + unlimited arguments in the constructor. Therefor I created a class with a constructor with dynamic arguments:


Code:
public class MyClass {

   public MyClass(Object... obj) {
       // some code
   }

}


However ... hibernate can't instantiate this class because he can't find the right constructor.

Can anyone tell me whether this is possible ?






Details:

Hibernate version: 3.0.0

Full stack trace of any exception that occurs:
Caused by: org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [be.retailability.tellit.selector.oasis.lifesearch.LiveSearchStringArray] [select new be.retailability.tellit.selector.oasis.lifesearch.LiveSearchStringArray(hobj.voornaam,hobj.naam,hobj.rijksregisterNummer) from be.ugent.oasis.model.administratie.Persoon hobj where upper(naam) like '%DE B%' order by hobj.voornaam,hobj.naam,hobj.rijksregisterNummer]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:235)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
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 org.springframework.orm.hibernate3.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1199)
at $Proxy41.createQuery(Unknown Source)
at be.retailability.util.hibernate.HibernateQueryCallbackFactoryImpl$LiveSearchCallback.doInHibernate(HibernateQueryCallbackFactoryImpl.java:301)
at be.retailability.util.hibernate.HibernateQueryCallbackFactoryImpl$LoggingCallback.doInHibernate(HibernateQueryCallbackFactoryImpl.java:140)
... 33 more


Name and version of the database you are using: Mysql 5

Debug level Hibernate log excerpt: debug


[/code]


Top
 Profile  
 
 Post subject: Re: Dynamic instantion + Java dynamic arguments
PostPosted: Mon Mar 10, 2008 11:59 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
leave a default constructor for hibernate.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 5:14 am 
Newbie

Joined: Mon Mar 10, 2008 8:55 am
Posts: 9
These are my constructors right now:

Code:
    public LiveSearchStringArray() {
        // TODO Auto-generated constructor stub
    }
   
    public LiveSearchStringArray(Object... obj) {
        this.objects=obj;
    }


Hibernate gives the same error though.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 10:53 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Ah, my bad. I didn't see the new thing in the query. I really think you should have a constructor that matches that one. Sorry again.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 11:01 am 
Newbie

Joined: Mon Mar 10, 2008 8:55 am
Posts: 9
Hmm, but that's going to give really ugly code :\
This is my dirty fix at the moment (it kinda makes me sick ;))

Code:
    public LiveSearchStringArray(Object key, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5) {
        this.objects = new Object[] {obj1, obj2, obj3, obj4, obj5};
        this.key = key;
    }
   
    public LiveSearchStringArray(Object key, Object obj1, Object obj2, Object obj3, Object obj4) {
        this.objects = new Object[] {obj1, obj2, obj3, obj4};
        this.key = key;
    }
   
    public LiveSearchStringArray(Object key, Object obj1, Object obj2, Object obj3) {
        this.objects = new Object[] {obj1, obj2, obj3};
        this.key = key;
    }
   
    public LiveSearchStringArray(Object key, Object obj1, Object obj2) {
        this.objects = new Object[] {obj1, obj2};
        this.key = key;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 11:09 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Umm, Object... implementation is in fact an array of Object[] right? I am wondering if you could modify the sql to call the constructor with an array of objects and see if it works. It might work.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 12:19 pm 
Newbie

Joined: Mon Mar 10, 2008 8:55 am
Posts: 9
Hmm good idea .. I'll try it out and keep you posted


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 4:00 am 
Newbie

Joined: Mon Mar 10, 2008 8:55 am
Posts: 9
Aah too bad ... hibernate trips over the braces in its query:

Code:
unexpected char: '{' [select new be.retailability.util.model.LiveSearchStringArray(rijksregisternummer,new Object[] {hobj.rijksregisterNummer,hobj.naam,hobj.voornaam}) from be.ugent.oasis.model.administratie.Persoon hobj where upper(naam) like '00000%' order by hobj.rijksregisterNummer,hobj.naam,hobj.voornaam]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 5:53 am 
Newbie

Joined: Wed Apr 23, 2008 5:46 am
Posts: 1
Yes! Very important! Leave the default constructor also there!

ex.:
Code:
MyClass {
public MyClass(){}

public MyClass(Object ob1, Object ob2,...)
{ ..... }

}


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