-->
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: Tests with Projection API
PostPosted: Tue Feb 08, 2005 1:55 pm 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
I'm trying to adapt a CriteriaPager I've used with the patched version of Hibernate2 to page criteria query results.
This components simply get a Criteria object, applies a RowCount projection to the query, executes it, gets the total row number for paging calculation, removes the projection, sets the MaxResults and FirstResult params and re-executes the query. When I execute the first query, I get an Exception:
Code:
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of web.model.Articolo.id
        at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:117)
        at org.hibernate.persister.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:673)
        at org.hibernate.persister.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:845)
        at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:169)
        at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:202)
        at org.hibernate.type.ManyToOneType.disassemble(ManyToOneType.java:98)
        at org.hibernate.cache.StandardQueryCache.put(StandardQueryCache.java:63)
        at org.hibernate.loader.Loader.list(Loader.java:1333)
        at org.hibernate.loader.CriteriaLoader.list(CriteriaLoader.java:106)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1603)
        at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:257)
        at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:373)
        at web.pager.CriteriaPager.init(CriteriaPager.java:72)
        at web.pager.CriteriaPager.getPageElements(CriteriaPager.java:130)
        at web.action.RicercaArticoloAction.go(RicercaArticoloAction.java:170)
        at web.action.AbstractAction.execute(AbstractAction.java:190)
        at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:169)
[snip]


The code I execute:
Code:
criteria.setProjection( Projections.rowCount() );
            int elementsSize = 0;
            try {
                elementsSize = Integer.parseInt(criteria.uniqueResult().toString());
            } catch( Exception e ) {
                e.printStackTrace( System.out );
            }
            criteria.setProjection( null );


Any idea on this?
I'd like to receive comments on this component. I use it with WebWork, if I get this working I can contribute it, if anyone is interested.

Hibernate version:
Hibernate 3 beta 3

Mapping documents:
Articolo
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>

<class
    name="web.model.Articolo"
    table="ARTICOLO"
>
    <cache usage="read-write" />
    <id
        name="id"
        type="java.lang.String"
        column="ID"
    >
        <generator class="assigned" />
    </id>

    <property
        name="catalogo"
        type="java.lang.String"
        column="CATALOGO"
        length="64"
    />
[snip]
    <many-to-one
        name="genere"
        class="web.model.Genere"
        not-null="true"
    >
        <column name="GENEREID" />
    </many-to-one>
    <many-to-one
        name="autore"
        class="web.model.Autore"
        not-null="true"
    >
        <column name="AUTOREID" />
    </many-to-one>

</class>
</hibernate-mapping>


Autore
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>

<class
    name="web.model.Autore"
    table="AUTORE"
>
    <cache usage="read-write" />
    <id
        name="id"
        type="long"
        column="ID"
    >
        <generator class="assigned" />
    </id>

    <property
        name="nome"
        type="java.lang.String"
        column="NOME"
        length="256"
    />
    <property
        name="descrizione"
        type="java.lang.String"
        column="DESCRIZIONE"
        length="1024"
    />
    <property
        name="posizione"
        type="java.lang.String"
        column="POSIZIONE"
        length="256"
    />
    <set
        name="articoloList"
        lazy="true"
        inverse="true"
      cascade="none"
    >
        <key>
            <column name="AUTOREID" />
        </key>
        <one-to-many
            class="web.model.Articolo"
        />
    </set>

</class>
</hibernate-mapping>


Genere
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
<class
    name="web.model.Genere"
    table="GENERE"
>
    <cache usage="read-write" />
    <id
        name="id"
        type="long"
        column="ID"
    >
        <generator class="assigned" />
    </id>

    <property
        name="nome"
        type="java.lang.String"
        column="NOME"
        length="64"
    />
    <set
        name="articoloList"
        lazy="true"
        inverse="true"
      cascade="none"
    >
        <key>
            <column name="GENEREID" />
        </key>
        <one-to-many
            class="web.model.Articolo"
        />
    </set>
    <many-to-one
        name="categoria"
        class="web.model.Categoria"
        not-null="true"
    >
        <column name="CATEGORIAID" />
    </many-to-one>

</class>
</hibernate-mapping>



Name and version of the database you are using:
Oracle DB 9.2.0.5

The generated SQL (show_sql=true):
Code:
Hibernate: select count(*) as y0_ from ARTICOLO this_ where lower(this_.ID) like ?
[/b]

_________________
--
Marco


Top
 Profile  
 
 Post subject: More info on this: possible Hibernate 3 bug!
PostPosted: Wed Feb 09, 2005 6:04 am 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
I think I've figured out what's wrong.
I've enabled the cache on the criteria (setCacheable(true)), so Hibernate tries to get the Id of the returned record for cache handling, but it fails 'cause the id column is not available (projection!):

Stack trace:
Code:
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of cinetica.nannucci.web.model.Articolo.id
        at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:117)
        at org.hibernate.persister.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:673)
        at org.hibernate.persister.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:845)
        at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:169)
        at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:202)
        at org.hibernate.type.ManyToOneType.disassemble(ManyToOneType.java:98)
**************
        at org.hibernate.cache.StandardQueryCache.put(StandardQueryCache.java:63)
**************
        at org.hibernate.loader.Loader.list(Loader.java:1333)
        at org.hibernate.loader.CriteriaLoader.list(CriteriaLoader.java:106)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1603)
        at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:257)
        at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:373)
[snip]
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
        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:324)
        at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:103)
        ... 43 more


BTW, is there a way to obtain a copy of the criteria object, so I can set the projection and cache policy without altering the original criteria?

_________________
--
Marco


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 09, 2005 6:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
oh, yes. criteria query caching dont support projection yet. woops.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 09, 2005 7:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Fixed in CVS.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 11, 2005 5:23 am 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
OK, it works ! Thanks

_________________
--
Marco


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.