0

I have generic abstract superclass GenericModel with method that I would like to call in children classes as follow:

public abstract class GenericModel<T extends GenericModel> {
    @Transient protected Class<T> entityClass;
    public GenericModel() {
    Class obtainedClass = getClass();
    Type genericSuperclass = null;
    for(;;) {
        genericSuperclass = obtainedClass.getGenericSuperclass();
        if(genericSuperclass instanceof ParameterizedType) {
            break;
        }
        obtainedClass = obtainedClass.getSuperclass();
    }
    ParameterizedType genericSuperclass_ = (ParameterizedType) genericSuperclass;
    entityClass = ((Class) ((Class) genericSuperclass_.getActualTypeArguments()[0]));

    // some code

    public List<T> getByCreationUser_Id(Long id) {
        // method body
        return entities;
    }
}

I am extending class this class with:

public class Insurance extends GenericModel<Insurance> {
     // some code
}

And I wanna extend class: Insurance with this class as follow:

public class Liability extends Insurance {

}

and object of Liability class I would like call method: getByCreationUser_Id() and instead of list of T I would like to get List<Liability> as follow:

List<Liability> currentUserInsurances = new Liability().getByCreationUser_Id(1L);

Unfortunatelly I am getting an error:

Type mismatch: cannot convert from List<Insurance> to List<Liability>

When I will call same method on Insurance class it works, I am getting an: List<Insurance>

I've already tried declare classes as follow:

public class Insurance<T> extends GenericModel<Insurance<T>> {}
public class Liability extends Insurance<Liability> {}

But id doesn't work. Compile errors occur.

Please help.

1
  • 1
    You already got a good answer, I just want to hint that you might want to avoid raw types, so declare your class as GenericModel<T extends GenericModel<T>>. Commented Jul 22, 2015 at 8:38

1 Answer 1

3

You need to declare your Insurance class as,

public class Insurance<T extends Insurance> extends GenericModel<T> {}

And then, your Liability class as,

public class Liability extends Insurance<Liability> {}
Sign up to request clarification or add additional context in comments.

5 Comments

How to initialise java objects for this classes?
Ok, but I am getting an error: Caused by: org.hibernate.InstantiationException: could not instantiate test objectmyInsurance.models.Insurance from JPA. Can You help me?
I think that's related to hibernate framework. And I am not good at it. But it's not related to the Generics. You can ask a new question with the exception trace and the relevant code. Make sure to tag hibernate in that.
I've added new post here. Can You tell me is it possible to store the structure/ class declaration from Your answer in db using jpa persistence?
It has nothing to do with JPA - it's just that the genericSuperclass_.getActualTypeArguments()[0] will return a TypeVariable for Insurance now - instead of the expected Class. I posted the solution in your second question.

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.