4

I have a one-to-many class member of type List< GenericType < T > >. What is the recommended way to annotate this member in Hibernate?

Morbid details:

@Entity
public class DvOrdered extends DataValue
{
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name="dv_ordered_other_reference_ranges", 
        joinColumns = @JoinColumn(name = "reference_range_id"),
        inverseJoinColumns = @JoinColumn(name = "dv_ordered_id"))
    private List<ReferenceRange<T>> otherReferenceRanges;
}

I get

Caused by: org.hibernate.AnnotationException: Property com.safehis.ehr.rm.datatypes.quantity.DvOrdered.otherReferenceRanges has an unbound type and no explicit target entity. Resolve this Generic usage issue or set an explicit target attribute (eg @OneToMany(target=) or use an explicit @Type
at org.hibernate.cfg.PropertyContainer.assertTypesAreResolvable(PropertyContainer.java:140)
at org.hibernate.cfg.PropertyContainer.getProperties(PropertyContainer.java:118)
at org.hibernate.cfg.AnnotationBinder.addElementsOfClass(AnnotationBinder.java:1554)
at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:236)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:775)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3799)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)

Thanks in advance for any reply.

6
  • What have you tried and whats the error that you are getting? As a side note do you really have oneToMany with a generic type !!!! Commented May 5, 2015 at 22:28
  • Could you please provide the involved classes? Commented May 5, 2015 at 22:36
  • Added details about one error, Commented May 5, 2015 at 22:44
  • Error makes perfect sense . It does not know which concrete entity the relationship is with . Commented May 5, 2015 at 23:27
  • 1
    I'm sure the error makes sense. The question however asks how to write this type of idiom. Commented May 5, 2015 at 23:32

1 Answer 1

1

The generic information is lost at compile-time due, to type erasure so Hibernate can't figure out what type T is in your case.

You can change your code to this:

private List<ReferenceRange<T>> otherReferenceRanges;

And then use inheritance so that each ReferenceRange subclass embeds a certain type.

public abstract class ReferenceRange<T> {

    public abstract T getReference();
}

public class IntegerReferenceRange extends ReferenceRange<Integer> {

    private Integer reference;

    @Override
    public Integer getReference() {
        return reference;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply, but I don't understand how this helps. Is there a way to use Hibernate AnyMetaDef with a OneToMany association and then use, say, ReferenceRange < Integer > as you suggest as a MetaValue?
Using @AnyMetaDef gives you the same effect as if using inheritance, but you won;t be able to use nested generic types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.