Persist collection of interface using Hibernate
        Posted  
        
            by Olvagor
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Olvagor
        
        
        
        Published on 2010-05-26T12:39:07Z
        Indexed on 
            2010/05/26
            12:41 UTC
        
        
        Read the original article
        Hit count: 375
        
I want to persist my litte zoo with Hibernate:
@Entity
@Table(name = "zoo") 
public class Zoo {
    @OneToMany
    private Set<Animal> animals = new HashSet<Animal>();
}
// Just a marker interface
public interface Animal {
}
@Entity
@Table(name = "dog")
public class Dog implements Animal {
    // ID and other properties
}
@Entity
@Table(name = "cat")
public class Cat implements Animal {
    // ID and other properties
}
When I try to persist the zoo, Hibernate complains:
Use of @OneToMany or @ManyToMany targeting an unmapped class: blubb.Zoo.animals[blubb.Animal]
I know about the targetEntity-property of @OneToMany but that would mean, only Dogs OR Cats can live in my zoo.
Is there any way to persist a collection of an interface, which has several implementations, with Hibernate?
© Stack Overflow or respective owner