tags:

views:

52

answers:

1

hello, does anyone here knows the correct way of associating a single entity(a pojo class) to multiple classes.. im currently working on a situation where mainClass has a one-to-many relationship to subClass and subClass has one-to-many relationship to unitsClass too. the relationship of these classes looks like this:

  • mainClass - oneToMany - subClass
  • subClass - manyToOne - mainClass AND oneToMany - unitsClass
  • unitsClass - manyToOne - subClass

i dont know if its possible for subClass to contain multiple associations to multiple classes.if not, what would be the best way to address this problem? im using hibernate annotations.

hope someone could help me out on this.

thank you so much!

A: 

Yes. In the simplest form it would look like this:

@Entity
public class MainClass {
   @OneToMany
   private List<SubClass> subclasses;

   // Id and other fields
}

@Entity
public clsas SubClass {
    @ManyToOne
    private MainClass mainClass 

    @OneToMany
    private List<UnitClass> unitClasses;
}

@Entity
public class UnitClass {
    @ManyToOne
    private SubClass subClass;
}
Bozho
thanks. i've constructed the 3 classes this way and it works fine for the main class and subClass but i always get a NUllPointerException for the unitClass.
unknown
thanks! finally, i figured out the reason why NullPointerException occurs. thanks for you help again:P
unknown
@unknown if an answer works for you, mark it as accepted
Bozho