0

I am trying to solve a problem regarding category with child categories and parent category on same entity. My database is already set and I can't change it. So, I have mapped my entity this way:

public class Category implements Serializable {

    private static final long serialVersionUID = -3432724244623524272L;

    @Id
    @Column(name = "id")
    private Long id;

    @Column(name = "key", nullable = false)
    private String key;

    @Column(name = "category_name", nullable = false)
    private String name;

    @Column(name = "description")
    private String description;

    @ManyToOne(targetEntity = Category.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "parent_key", referencedColumnName = "key")
    private Category parentCategory;

    @OneToMany(mappedBy = "parentCategory", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Category> childCategories;

    //getters and setters ommited
}

Note, that the child categories and parent category is not mapped using the ID attribute, but the "key" attribute. This "key" is not a FK. When JPA is trying to get the data, my application crash. But this crash is look like an infinite loop. No exception is thrown.

What am I doing wrong?

humungs
  • 1,144
  • 4
  • 25
  • 44
  • You might be interested in this Q: http://stackoverflow.com/questions/5818373/does-the-jpa-specification-allow-references-to-non-primary-key-columns – zbig Sep 04 '14 at 11:38
  • Just a suggestion, but I'd say do a trial run, first setting fetch to FetchType.LAZY. – Tassos Bassoukos Sep 04 '14 at 11:59

1 Answers1

0

Possible infinite loop :

  1. You load an object A
  2. This object has a child B, which is loaded as well since you use FetchType.EAGER
  3. B has a parent, which is A, which is loaded again
  4. etc.

Try using FetchType.LAZY.

WilQu
  • 7,131
  • 6
  • 30
  • 38