1

Using Hibernate and JPA I want to map a class which contains a list of its own objects, something like:

public class Category{
     private List<Category> subCategories = new ArrayList<Category>();
}

I am trying to do this by using:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "category")

but when I try this I get the following error:

Initial SessionFactory creation failed.org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: my.domain.name.Category.category in my.domain.name.Category.subCategories
MoienGK
  • 4,544
  • 11
  • 58
  • 92
  • 2
    What you want to do is called "Self referencing entity". Check out this answer: http://stackoverflow.com/a/3393662/870122 – perissf Apr 02 '12 at 18:49
  • yep that was exactly my case, but i am getting this error now : Initial SessionFactory creation failed.org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer] – MoienGK Apr 02 '12 at 19:11

2 Answers2

1

The mappedBy attribute of the @OneToMany annotation refers to the property name of the association on the owners side. Apparently, there's no attribute named category in your Category class.

Carlos Gavidia-Calderon
  • 7,145
  • 9
  • 34
  • 59
1

Try this:

public class Category {

    @ManyToOne
    private Category superCategory;

    @OneToMany(mappedBy = "superCategory")
    private List<Category> subCategories = new ArrayList<Category>();
}

The problem in your example is that there is no such property category in your class.

darrengorman
  • 12,952
  • 2
  • 23
  • 24
  • i tried that and i am getting this error now : Initial SessionFactory creation failed.org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer] – MoienGK Apr 02 '12 at 19:11
  • @dave do you have any other exceptions from the stack trace? You do of course need the `@Entity` annotation on the `Category` class and I assume there are other attributes being mapped that you didn't include in the original question – darrengorman Apr 02 '12 at 19:27
  • @dave examine the stack trace for the root cause exception. Can I suggest that you accept my answer for this question (your mapping issue is resolved) and create a new question if necessary to get assistance with this new exception. – darrengorman Apr 02 '12 at 19:49