2

Hibernate 4 の使用中にいくつか質問があります。双方向のセルフ マッピング POJO もあります。親カテゴリから子カテゴリ リストを取得できますが、子カテゴリから親カテゴリを取得できません。 これが私のコードです:

@Entity
public class Category {

private Long id;
private String cateName;
private String cateType;
private String description;
private List<Category> childCategories;
private Category category;

public Category() {
    childCategories = new ArrayList<Category>();
}

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}

@ManyToOne
@JoinColumn(name="parent_id", referencedColumnName="id")
public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}

public String getCateName() {
    return cateName;
}
public void setCateName(String cateName) {
    this.cateName = cateName;
}
public String getCateType() {
    return cateType;
}
public void setCateType(String cateType) {
    this.cateType = cateType;
}
public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

@OneToMany(mappedBy = "category")
public List<Category> getChildCategories() {
    return childCategories;
}

public void setChildCategories(List<Category> childCategories) {
    this.childCategories = childCategories;
}

@Override
public String toString() {
    return "Category [id=" + id + ", cateName=" + cateName + ", cateType="
            + cateType + ", description=" + description + "]" + "Parent: " + category;
}

}

これは、通常のネストされたカテゴリ マッピングです。category.getChildCategories()通常はを使用して子リストを取得できますが、 を使用するcategory.getCategory()と、次のように id のみを含む親オブジェクトが返され、その他は null になります[id=7, cateName='Java', cateType='2', description='java category', category=[id='2', cateName=null, cateType=null, description=null, category= null]]。ルートまですべての親オブジェクトを取得する方法は? 本当にあなたの助けが必要です、ありがとう! 追加:categoryは次の方法で取得されます:
1. 一時オブジェクトを作成しますcategory
。 2.saveOrUpdate()メソッドを使用して永続化します。
3.getIdentifier()追加されたカテゴリの ID を返すために使用します。
4. を使用get()して、このカテゴリ オブジェクトをすぐに取得します。
それで、オブジェクトをすぐに取得すると、休止状態がキャッシュされた永続オブジェクトを返した場合、それは永続化されたと思いましたか?

4

1 に答える 1