Note that there are some explanatory texts on larger screens.

plurals
  1. POHow would I map a parent/child relation on same object with JPA
    primarykey
    data
    text
    <p>After reading this post <a href="https://stackoverflow.com/questions/2743895/jpa-map-relation-entity-parentid">JPA map relation entity parentID</a> I tried applying this to my code but this didn't work for me.</p> <p>This is the code I have in my Object</p> <pre><code>@Entity public class Category extends Model { public static final int EASY = 1; public static final int MEDIUM = 2; public static final int HARD = 3; public static final int VERRY_HARD = 4; public String name; public String fullName; public boolean active; public Date createdOn; public int difficulty; @ManyToOne @JoinColumn(name = "FK_PARENT_CATEGORY") public Category parentCategory; @OneToMany(mappedBy="parentCategory", cascade = CascadeType.ALL) public List&lt;Category&gt; subCategories; public Category(Category parentCategory, String name, boolean active) { this.name = name; this.active = active; this.parentCategory = parentCategory; this.subCategories = new ArrayList&lt;Category&gt;(); this.createdOn = new Date(); this.difficulty = Category.EASY; this.fullName = name; if (parentCategory != null) this.fullName = parentCategory.fullName + "/" + this.fullName; } </code></pre> <p>Now this is the test I run</p> <pre><code>@Test public void testParentAndSubCategories() { //Create the parent category new Category(null, "Sport", true).save(); Category sportCat = Category.find("byName", "Sport").first(); //Test the newly created parent category state assertNotNull(sportCat); assertEquals("Sport", sportCat.name); assertEquals(true, sportCat.active); assertEquals("Sport", sportCat.fullName); assertNull(sportCat.parentCategory); assertEquals(0, sportCat.subCategories.size()); //Create the subCategory new Category(sportCat, "Hockey", false).save(); Category hockeyCat = Category.find("byName", "Hockey").first(); // Test the newly created sub category assertNotNull(hockeyCat); assertEquals("Hockey", hockeyCat.name); assertEquals(false, hockeyCat.active); assertEquals("Sport/Hockey", hockeyCat.fullName); assertNotNull(hockeyCat.parentCategory); assertEquals("Sport", hockeyCat.parentCategory.name); assertEquals(0, sportCat.subCategories.size()); //Fetch new values for parent category sportCat = Category.find("byName", "Sport").first(); // Test updated parent category assertEquals(1, sportCat.subCategories.size()); assertEquals("Hockey", sportCat.subCategories.get(0).name); } </code></pre> <p>This line of the test always fail.</p> <pre><code>// Test updated parent category assertEquals(1, sportCat.subCategories.size()); </code></pre> <p>Based on the setup I have for my relations, Hibernate can't retrieve the subcategory and I don't know why. Now I really really hope this is not something stupid on my part because I'm going to have shoot myself (Even if it's late and I'm tired). By the way don't mind the public variables in the code, I'm using play!(playframework) and it takes care of encapsulation. Thanks in advance for any help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload