我将如何使用JPA将同一对象上的父/子关系进行映射 [英] How would I map a parent/child relation on same object with JPA

查看:115
本文介绍了我将如何使用JPA将同一对象上的父/子关系进行映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看完这篇文章后 JPA地图关系实体parentID 我试过把这个应用到我的代码,但这不适用于我。

After reading this post JPA map relation entity parentID I tried applying this to my code but this didn't work for me.

这是我在我的对象中的代码

This is the code I have in my Object

@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<Category> subCategories;

public Category(Category parentCategory, String name, boolean active) {
    this.name = name;
    this.active = active;
    this.parentCategory = parentCategory;
    this.subCategories = new ArrayList<Category>();
    this.createdOn = new Date();
    this.difficulty = Category.EASY;
    this.fullName = name;
    if (parentCategory != null)
        this.fullName = parentCategory.fullName + "/" + this.fullName;

}

现在这是我运行的测试

@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);

}

这行测试总是失败。

// Test updated parent category
    assertEquals(1, sportCat.subCategories.size());

基于我的关系设置,Hibernate无法检索子类别,不知道为什么。现在我真的很希望这不是我的愚蠢行为,因为我会拍摄自己(即使它迟到,我很累)。顺便说一下,不要介意代码中的公共变量,我正在使用play!(playframework)并且它负责封装。提前感谢您的帮助

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

推荐答案

这不是父类和子类映射到同一个类的问题。 - 问题是你需要手工维护双向关系的两端。

It is not a problem of mapping parent and child to the same class. - The problem is that you need to maintain both ends of the bi-directional-relationship by hand.

child.setParent(parent)
parent.addChild(child)

顺便说一句:一个负责将关系存储在数据库中),存储和重新加载实体也将在某些情况下起作用。 (在许多老教程中你会发现这个肮脏的伎俩)。但在我看来这是不好的做法。 (在你的测试用例中,在保存子文件之前重新加载父文件之前,需要清理缓存。)

BTW: Setting it only on one side (the one which is responsible to store the relationship in the database), store and reload the entity will work in some cases too. (And you will find this dirty trick in many old tutorials). But in my opinion it is bad practice. (In your test case, it would require to clean the cache before you reload the parent after the child is saved.)

这篇关于我将如何使用JPA将同一对象上的父/子关系进行映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆