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?