-->
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.  [ 2 posts ] 
Author Message
 Post subject: Sorting sets using order-by
PostPosted: Wed Jan 23, 2008 8:54 pm 
Newbie

Joined: Sun Jan 20, 2008 6:39 pm
Posts: 4
I'm trying to use order-by sorting to create a SortedSet, but I'm getting IllegalArgumentExceptions, saying that my set is of type PersistentSet, not SortedSet. Using sort="natural" works fine, but I can't get it to work using "order-by".

The following is the stack trace for setting a set, but I also get errors loading a set if it's already in the database.

Hibernate version:
3.2.5

Mapping documents:

<class name="testhibernate.A" table="A">
<id name="id">
<generator class="native"></generator>
</id>
<set name="mySet" cascade="all" order-by="value">
<key column="A_id" />
<one-to-many class="testhibernate.B" />
</set>
</class>
<class name="testhibernate.B" table="B">
<id name="id" column="B_ID">
<generator class="native"/>
</id>
<property name="value" />
</class>

Code between sessionFactory.openSession() and session.close():

Transaction tx = session.beginTransaction();

//Create and save an A.
A a = new A();
session.save(a);
session.flush();

//Create and save two B's
B b1 = new B();
B b2 = new B();
b1.setValue(new BigDecimal(11) );
b2.setValue(new BigDecimal(12) );
session.save(b1);
session.save(b2);
session.flush();

//Add these B's to the A.
SortedSet<B> s = new TreeSet<B>();
s.add(b1);
s.add(b2);
a.setMySet(s);

tx.commit();


Full stack trace of any exception that occurs:

Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of testhibernate.A.mySet
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:104)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3566)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:133)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at testhibernate.Test.main(Test.java:34)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
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:597)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:42)
... 11 more

Name and version of the database you are using:

mysql, 5.0.45

The generated SQL (show_sql=true):

18:02:39,580 DEBUG SQL:401 - insert into A values ( )
18:02:39,618 DEBUG SQL:401 - insert into B (value) values (?)
18:02:39,619 DEBUG BigDecimalType:133 - binding '11' to parameter: 1
18:02:39,621 DEBUG SQL:401 - insert into B (value) values (?)
18:02:39,622 DEBUG BigDecimalType:133 - binding '12' to parameter: 1

Debug level Hibernate log excerpt:

18:04:28,965 DEBUG AbstractFlushingEventListener:154 - dirty checking collections
18:04:28,965 DEBUG AbstractFlushingEventListener:171 - Flushing entities and processing referenced collections
18:04:28,976 DEBUG WrapVisitor:87 - Wrapped collection in role: testhibernate.A.mySet
18:04:28,976 ERROR BasicPropertyAccessor:94 - IllegalArgumentException in class: testhibernate.A, setter method of property: mySet
18:04:28,977 ERROR BasicPropertyAccessor:98 - expected type: java.util.SortedSet, actual value: org.hibernate.collection.PersistentSet


Top
 Profile  
 
 Post subject: Using order-by and sort=
PostPosted: Thu Jan 24, 2008 7:48 pm 
Newbie

Joined: Sun Jan 20, 2008 6:39 pm
Posts: 4
If I change the <set> tag to include both order-by="value" and sort="natural", I do not get the same error. The SQL is correct, it uses order-by; however, it of course sorts it afterward in java, which I was trying to avoid.

.hbm.xml:
Quote:
<set name="mySet" table="B" cascade="all" order-by="value" sort="natural">

SQL:
Quote:

DEBUG SQL:401 - select myset0_.A_id as A3_1_, myset0_.B_ID as B1_1_, myset0_.B_ID as B1_1_0_, myset0_.value as value1_0_ from B myset0_ where myset0_.A_id=? order by myset0_.value


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