-->
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.  [ 1 post ] 
Author Message
 Post subject: Cast Entity association collection to implementation type
PostPosted: Thu May 21, 2009 6:30 pm 
Newbie

Joined: Thu May 21, 2009 6:25 pm
Posts: 2
Persistence Provider: JBoss Hibernate

I have an entity with a one-to-many association. For my business logic, I want the collection implementation type to be a TreeSet as I can sort it.

Code:
@Entity
public class Foo1 {
  @Id
  private Long id;
  @OneToMany(mappedBy="foo1");
  Set<Foo2> foo2s;

  public void addFoo2(Foo2 foo2) {
    if (foo2s == null) {
      this.foo2s = new TreeSet<Foo2>();
    }
    this.foo2s.add(foo2);
  }
  ...
}

Let's say I persist the entity and so it becomes managed. When the entity becomes managed, the persistence provider replaces the collection implementation with its custom implementation org.hibernate.collection.PersistentSet. Now if I do any TreeSet operation on my collection, it does not work as the collection implementation type is not a TreeSet anymore.

Code:
((TreeSet<Foo2>)foo1.getFoo2s()).first() //throws casting exception.

Exception:
Code:
org.hibernate.collection.PersistentSet cannot be cast to java.util.TreeSet

Declaring the collection as java.util.SortedSet did not work either. It threw exception on startup.
Code:
Illegal attempt to map a non collection as a @OneToMany

But I do want my collection implementation to be a TreeSet even in a managed entity. I know I can not cast org.hibernate.collection.PersistentSet to java.util.TreeSet.

Why do we have such restriction in implementation? Why can't the persistence provider (Hibernate) use TreeSet as the implementation type?

I do not want my code to be dependent on vendor-specific implementation like PersistentSet or PersistentSortedSet. Is there a solution to this? Is there a way to use a java.util.* collection implementation in a managed entity?

I would appreciate any help.

Thanks
Vignesh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.