2

I have a generic class which is a superclass of some non-generic class and those are just setting its generic parameter like this:

@ManagedProperty
class A<T>{
    @Id
    getId(){..}
    setID(int id){..}
    int id

    T t;
    T getT(){...}
    setT(T t){...}
}

and

@Entity
class B extends A<Integer>{}

but I get MappingException which says:

It has an unbound type and no explicit target entity. Resolve this Generic usage issue or set an explicit target attribute.

how should I fix it?

1

1 Answer 1

0

Your example can't work. getT() is defined as package private, this means, it is not visible in the subclass B. When Hibernate parses the class with reflections it can find the method getT(), but as this method is not visible in the subclass, reflections does not provide the type for it.

What you can do:

  1. Define getT() as public or at least as protected. (I'm not sure if this will solve your problem.)
  2. Use the @Type annotation to define the concrete type for Hibernate (thought then you'll perhaps loose the advantage which you gained with your generic class)
  3. Use an xml configuration file instead of annotations for class B. There you'll define the configuration for class B, and there you can even set the type of members and getters which are only defined as private or package private in a superclass.
  4. In addition to 1. you define the method getT() in B too and there you just call super.getT().

I always do solution 3 - I don't like annotations anyway ;-)

Sign up to request clarification or add additional context in comments.

Comments

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.