Hibernate mapping for list which is in different class

51 views Asked by At

I have class structure like below

class Animal{
int animalId;
Dogs dogs;

//setters and getters
}

class Dogs{
List<Dog> dogs;
getDogsList(){
...
}
setDogsList(){
...
}
}

class Dog{
int animalId;
String name;
}

Now I have to map oneToMany between Animal and List entity which is in different class Dogs. I know this is a bad design but I have to deal with it. Ideally it should have been

class Animal{
int animalId;
List<Dog> dogs;
//setters and getters
}

but all these pojos comes from an xml file which I can not modify. is there any way for me to map oneToMany between Animal and Dog (ignoring class Dogs)?

1

There are 1 answers

0
EasterBunnyBugSmasher On

you can probably do it like this:

@Entity
public class Animal {

    @Id
    protected long animalId;

    @Transient
    protected Dogs dogs;

    public Animal() {
       this.dogs = new Dogs();
    }

    @Access(AccessType.property)
    public List<Dog> getDogs() {
        return dogs.getDogList();
    }

    public void setDogs(List<Dog> dogs) {
        this.dogs.setDogList(dogs);
    }
}