-->
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.  [ 10 posts ] 
Author Message
 Post subject: Problem with "select new" HQL clause
PostPosted: Wed Dec 17, 2003 1:55 am 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Folks,

When my DTO object that I am "Select new"'ing into has a constructor that takes native types, I get the error attached.

If my DTO has a ctor which takes wrapper types, the code works.

If it has both, then Hibernate seems to prefer the native version, and I get the error.

I tried implementing the native-type ctor in two ways, using the "this" keyword to delegate to the wrapper-type ctor or with direct calls to the setter methods on the DTO. Didn't seem to make any difference.


I'm using nightly build 20031214.

Is this worth creating a JIRA issue for?





Code:
    [junit] Testcase: testFindDocumentsByCaseDTOsWithEmptyCriteria(nrm.clas.persistence.document.test.DocumentPersistenceManagerTest):  Caused an ERROR
    [junit] Error finding documents by case
    [junit] nrm.clas.persistence.PersistenceLayerException: Error finding documents by case
    [junit]     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    [junit]     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    [junit]     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    [junit]     at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
    [junit]     at nrm.clas.ExceptionUtil.instantiateException(ExceptionUtil.java:89)
    [junit]     at nrm.clas.ExceptionUtil.createNestedException(ExceptionUtil.java:127)
    [junit]     at nrm.clas.ExceptionUtil.createPersistenceLayerException(ExceptionUtil.java:312)
    [junit]     at nrm.clas.ExceptionUtil.createPersistenceLayerException(ExceptionUtil.java:291)
    [junit]     at nrm.clas.persistence.document.DocumentPersistenceManagerImpl.findDocumentsByCaseDTOs(DocumentPersistenceManagerImpl.java:162)
    [junit]     at nrm.clas.persistence.document.test.DocumentPersistenceManagerTest.testFindDocumentsByCaseDTOsWithEmptyCriteria(DocumentPersistenceManagerTest.java:45)
    [junit]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [junit]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    [junit]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    [junit]     at nrm.clas.test.ClasTestCase.run(ClasTestCase.java:508)
    [junit] Caused by: nrm.clas.common.persistence.PersistenceException: find
    [junit]     at nrm.clas.persistence.BasePersistenceManagerImpl.handleError(BasePersistenceManagerImpl.java:97)
    [junit]     at nrm.clas.persistence.BasePersistenceManagerImpl.handleError(BasePersistenceManagerImpl.java:116)
    [junit]     at nrm.clas.persistence.BasePersistenceManagerImpl.find(BasePersistenceManagerImpl.java:365)
    [junit]     at nrm.clas.persistence.document.DocumentPersistenceManagerImpl.findDocumentsByCaseDTOs(DocumentPersistenceManagerImpl.java:159)
    [junit]     ... 16 more
    [junit] Caused by: net.sf.hibernate.QueryException: could not instantiate: class nrm.clas.dto.document.CaseDocumentDTO
    [junit]     at net.sf.hibernate.hql.QueryTranslator.getResultColumnOrRow(QueryTranslator.java:993)
    [junit]     at net.sf.hibernate.loader.Loader.doQuery(Loader.java:221)
    [junit]     at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:132)
    [junit]     at net.sf.hibernate.loader.Loader.doList(Loader.java:949)
    [junit]     at net.sf.hibernate.loader.Loader.list(Loader.java:940)
    [junit]     at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:833)
    [junit]     at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1475)
    [junit]     at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1454)
    [junit]     at nrm.clas.persistence.BasePersistenceManagerImpl.find(BasePersistenceManagerImpl.java:359)
    [junit]     ... 17 more
    [junit] Caused by: java.lang.NullPointerException
    [junit]     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    [junit]     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    [junit]     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    [junit]     at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
    [junit]     at net.sf.hibernate.hql.QueryTranslator.getResultColumnOrRow(QueryTranslator.java:990)
    [junit]     ... 25 more

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 9:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
It is a know limitation, have a look throuhg JIRA to see if you can find an entry if not yes create one so its documented.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 9:39 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Couldn't find anything, so I raised HB-567.

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject: select new
PostPosted: Mon Jan 05, 2004 2:43 pm 
Newbie

Joined: Wed Sep 24, 2003 12:14 pm
Posts: 16
Location: Brazil
Hi!

Any news here? We have the same problem...

Thanks a lot

Paulo Alvim - Powerlogic - Brazil

_________________
Paulo Alvim
Powerlogic - Brazil


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2004 6:52 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
It's caused by having null values in the result set.
Obviously (now, anyway :), you can't send a null reference to a constructor parameter that takes a primitive type.

So if one of the columns that you are querying is declared not-null and the query does nothing to stop null values in the result set for that column, then you'll be asking Hibernate to put null into this primitive value, which the language itself (never mind Hibernate) can't do.

If you need to be able to support null values in this situation then you'll need to use a ctor that takes a wrapper class instead.

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 5:31 pm 
Newbie

Joined: Wed Sep 24, 2003 12:14 pm
Posts: 16
Location: Brazil
Thank you, Stolley!...

I did know about the cause (null value to primitive...) but your information about the "ctor that takes a wrapper class instead." is very interesting. Is there any link that explain how to do that?

Alvim.

_________________
Paulo Alvim
Powerlogic - Brazil


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 7:46 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
What I mean is, if you have a ctor like this:

public SomeObject(long id, String value1, boolean value2){
this.id = new Long(id);
this.value1 = value1;
this.value2 = new Boolean(value2);
}

Change it to (you have to change it, or hibernate will choose the primitive version in preference):

public SomeObject(Long id, String value1, Boolean value2){
this.id = id;
this.value1 = value1;
this.value2 = value2;
}

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 1:01 pm 
Newbie

Joined: Wed Sep 24, 2003 12:14 pm
Posts: 16
Location: Brazil
Ok! Now I undestand that "ctor" is "creator"! I'm sorry I didn't know this word...

This would be the ideal. But I'm using 2.03 and Hibernate seems to try always "long" instead of recognize the wrapper "Long" in the ctor. Would it possible be a bug of this version? Or do I have to make anything else (I tried to put "java.lang.Long" in the mapping "type" attribute but it did't work yet)

Thanks once more...

_________________
Paulo Alvim
Powerlogic - Brazil


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 6:58 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
ctor is my shorthand for constructor, yeah.

The patch to allow this functionality was only applied for HB 2.1 and later, previous versions could only use constructors that took primitive types.

Issue 97 is the relevant enhancement, you could apply it your copy directly if you can't upgrade to 2.1 for some reason.

http://opensource.atlassian.com/project ... ?key=HB-97

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject: constructor
PostPosted: Fri Jan 09, 2004 7:53 am 
Newbie

Joined: Wed Sep 24, 2003 12:14 pm
Posts: 16
Location: Brazil
Sorry,

I'd like to say "constructor" instead of "creator"...

Thank you very much! This last reply is exactly what I was looking for.:-)

_________________
Paulo Alvim
Powerlogic - Brazil


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