Possible to map OneToMany as a discreet class?

I’ve been trying to figure out something but at run time I get:

class org.hibernate.collection.spi.PersistentBag cannot be cast to class a.b.c.MyClass

What I’d like to do is replace my OneToMany mappings like this (which works):

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = InfoEntityPhone.class)
public List<InfoEntityPhone> getInfoEntityPhoneCollection() {
	return infoEntityPhoneCollection;
}
public void setInfoEntityPhoneCollection(
		List<InfoEntityPhone> infoEntityPhoneCollection) {
	this.infoEntityPhoneCollection = infoEntityPhoneCollection;
}

So they use a discrete class instead so I can add extra functionality to the “collection” class.

public class InfoEntityPhoneCollection extends ArrayList implements List { …

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = InfoEntityPhone.class)
public InfoEntityPhoneCollection getInfoEntityPhoneCollection() {
	return infoEntityPhoneCollection;
}
public void setInfoEntityPhoneCollection(
		InfoEntityPhoneCollection infoEntityPhoneCollection) {
	this.infoEntityPhoneCollection = infoEntityPhoneCollection;
}

Is this impossible or am I missing something?

That’s not possible. A @OneToMany/@ManyToMany must by definition be a Collection or Map. If you want to store information in the join table, make it an entity e.g.

@Entity
class EntityA {
    @OneToMany(mappedBy = "entityA")
    Set<EntityAEntityBEntry> collection;
}
@Entity
class EntityAEntityBEntry {
    @Id
    @ManyToOne(fetch = LAZY)
    EntityA entityA;
    @Id
    @ManyToOne(fetch = LAZY)
    EntityB entityB;
}
@Entity
class EntityB {
    @OneToMany(mappedBy = "entityB")
    Set<EntityAEntityBEntry> collection;
}

No, I don’t want a join table. I just wanted to encapsulate the collection into a discreet class (which is a subclass of a collection, no?) so I could some further custom actions for the collection (like finding the one that’s marked primary or of type X). In this case, I’ll add the getter that returns the discreet class pre-populated with the collection if there are any entries (no need to delete/insert/update from the class).

Oh well, just thought I’d ask an expert!

Thanks!

In this case, I’ll add the getter that returns the discreet class pre-populated with the collection if there are any entries (no need to delete/insert/update from the class).

Sounds like the way to go.