tags:

views:

486

answers:

1

I am trying to map a JPA (using Hibernate now) Many-to-one relationship with a polymorphic type, but I am having no luck. I don't see why it isn't possible, or why I would be forced to declare a concrete type in the mapping. Here is an example:

@MappedSuperclass
class BaseClass {
    @Id
    long id;
}
class ClassWithList extends BaseClass {
    String attribute;

    @OneToMany(mappedBy="backPointer")
    List<ListClass> list;
}
class ListClass extends BaseClass {
    String listItemData;

    @ManyToOne
    @JoinColumns({
       @JoinColumn(name="baseId"),
       @JoinColumn(name="baseType"),
    })
    BaseClass backPointer;
}

Thanks for any advice you can give.

+1  A: 

You have nothing to say for a particular record, which type it is. I suggest you look at TopLink JPA: How-To Define Inheritance. It's equallyapplicable to Hibernate.

cletus
You are correct in that I haven't typed the column on the ListClass object, but I don't think the How-To Define Inheritance document is what I am looking to do. I think I can add the type column to the ListClass, but I guess I don't have a way for the JPA provider to know to use that type. I will add the @JoinColumns annotation to my example.
_Mike