Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] UPDATE: IndirectSet cannot be cast to java.util.TreeSet (Problem isn't fixed)

Hi Michael,

thanks for reply.

Yes, that was what I am afraid of. The bigger picture is, that I would like get the newest children without any performance issues. The children are holding a date in my real case. I implemented this as usual with a TreeSet, which will sort the children's by adding a new one automatigally with a comparator.

I might fetch the childrens sorted by the database and not in my java app, so I can use lazy load or batch fetch.

Unfortunately it is not an option to sort the HashSet at the Java-side each time I retrieve the result, that would be too slow.

Actually an ORM should'nt affect the OO side. But in this case I have to design my object architecture with regard to JPA. This isn't sufficient in my eyes. If I want to use a TreeSet, I should get a TreeSet and all other Collection types.

Thanks,

Martin.




Am 02.12.2009 um 22:25 schrieb Michael Bar-sinai:

Hi Martin,
Eclipselink creates an IndirectSet to do the lazy loading - TreeSets stores everything in the memory. You can't really get around this unless you do EAGER loading, and even then you'll probably get something that not a TreeSet.
What's the bigger picture? What are you trying to achieve? You could always instantiate a TreeSet and call addAll(), but that would be manually eager loading.

Michael Bar-Sinai

P.S.
The @OrderBy annotation applies to lists: http://www.eclipse.org/eclipselink/api/1.1/javax/persistence/OrderBy.html

On Wed, Dec 2, 2009 at 11:15 PM, Martin Dames <m.dames@xxxxxx> wrote:
Ok, I tried a bit and didn't get a solution:


I changed the fetch type to EAGER instead of LAZY.

Now I get another ClassCastException :
java.lang.ClassCastException: java.util.HashSet cannot be cast to java.util.TreeSet

The setter method from the last post (see below) will convert the passed Set into a TreeSet, so there should just be an instance of TreeSet in my children set, but EclipseLink is creating a HashSet !?!.

I tried to use OrderBy Annotation:

@OrderBy(value="firstname")
@OneToMany (mappedBy = "entity", fetch=FetchType.EAGER, cascade = {CascadeType.ALL})
private Set<Child> childrens = new TreeSet<Child>();

in hope that it will return an instance of TreeSet (which is sorted at first place in ascending order), but this does not work either.

Thank you for your support!

Martin.



Anfang der weitergeleiteten E-Mail:

Von: Martin Dames <m.dames@xxxxxx>
Datum: 2. Dezember 2009 20:08:55 MEZ
Betreff: IndirectSet cannot be cast to java.util.TreeSet

Hi all,

I've got an error when I look up my lazy loaded children of one of my pojos:

java.lang.ClassCastException: org.eclipse.persistence.indirection.IndirectSet cannot be cast to java.util.TreeSet

Ok, the thing has something to do with the lazy loading technology in EclipseLink/TopLink... if i have an entity like this:

@Entity
class Entity {

@OneToMany (mappedBy = "entity", fetch=FetchType.LAZY, cascade = {CascadeType.ALL})
private Set<Child> childrens = new TreeSet<Child>();

public void setChildrens(Set<Child> childrens) { this.childrens = childrens }

public Set<Child> getChildrens() { return this.childrens }

public Child getNextChild() {
return this. childrens.size() > 0 ? ((TreeSet<Child>) this. childrens).last() : null;
}
...
}

If I now find all entities of Entity and call getNextChild(), I will get the error. It seems that EclipseLink will pass a IndirectSet object into my childrens set and then I can not cast it anymore. The indirect set handles somehow the lazy load technique.... so what to do?

I tried this:
public void setChildrens(Set<Child> childrens) { this.childrens = new TreeSet<Child>(childrens); }

but this does not work either. It seems, that EclipseLink is using reflection to pass the IndiretSet into my set property.

I my assumptions are correct, what is to do if want to use lazy loading, a TreeSet (for sorting purposes--see .last() above) and the Set property of my Entity class interface should be remain java.uti.Set?

Thank you for help!

Martin.



_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top