I want to have a class in which I can indicate which class it extends. Something like
public class ComunAux extends T {
private MyObject obj
[...]
}
The opposite is not valid for me: that a class extends ComunAux and thus have that MyObject field
It could also be worth to me that the ComunAux class has a field of type T, something like:
public class ComunAux<T> {
private T parent;
[... otros campos comunes ...]
}
But in this last case, how do I create the parent instance in the ComunAux constructor?
IMPORTANT: working with Java 6 (1.6.0_45)
EDIT, before the doubts ...
In the second case, in the default constructor, without parameters, of ComunAux
, you would need to initialize the attribute T parent
so that it is not null, that is to say something like that (I know it's wrong): this.parent = new T()
, because I'm going to use it in a Hibernate query and I can not use the constructor that I want (or so I think)
Why do I need this? I have to obtain from database, with Hibernate, a list of objects (A), between the data obtained there is an entire entity (let's call it X) that I do not want to leave in the final list, but instead I leave a String which contains the concatenation of various fields of X (with a special logic of concatenation). To solve this I create an auxiliary class with the X field and extend A so that it has all the data of A:
class Aaux extends A {
X x;
//get/set de x
}
With Aaux
I make the query, obtaining a List<Aaux> listaAux
.
I go through all listaAux
, setting the A.xx field from the Aaux.X that I have obtained I create a new list List<A> result
:
List<A> result = new ....;
for(Aaux itemAux: listaAux){
itemAux.setX(concatenarX(itemAux.x);
result.add(itemAux);
//aqui ya me quedo solo con los datos de A, sin X
}
return result;
Now I have to repeat this in 3 more cases, each with a different entity: B, C and D. So I would have to create their corresponding auxiliary classes, all with the same X field but extending a different class: class Baux extends B class Caux extends C ...
My goal is to have a single auxiliary class that can tell you what your parent class is at the time you use it.
And for those who do not know how to hibernate ... I must indicate the class (class) to be returned, that is to say Aaux.class
, or ComunAux<A>.class
(as it has to be done)
Maybe I ask the impossible: (
Thanks for the answer and try:)