I had to deal with your problem as our client app was not allowed to have a dependency on hibernate (don't ask). What we ended up doing was something like this:
Code:
**
* Subclass of the Hibernate PersistentSet which serializes to a java.util.HashSet.
*/
public class SafePersistentSet extends PersistentSet
implements Serializable {
public SafePersistentSet() {
}
public SafePersistentSet(SessionImplementor session) {
super(session);
}
public SafePersistentSet(SessionImplementor session, Set set) {
super(session, set);
}
@SuppressWarnings({"unchecked"})
public Object writeReplace()
throws java.io.ObjectStreamException {
return new HashSet(this);
}
}
and then implemented a UserCollectionType to use this set (which requires more code than I originally expected). Of course, you must do this for every collection type that you use. There may also be some nasty bugs with regard to lazy loading, as we load everything up front in this case.
As emmanuel says, there are some very good reasons that hibernate replaces the collection implementations. In my experience, the most signifacant benefit is that it allows hibernate to keep track of deletes. As soon as you do something such as this, that all becomes your problem. Certainly not impossible, but not quite as automatic as things usually are.