Hibernate 4一対のマルチリレーションシップマッピング(自己関連)

5218 ワード

例:1つのカテゴリ(Category)の下に複数のサブカテゴリがあり、複数のサブカテゴリは同じ親カテゴリに属します.
public class Category  {

	private Integer id;
	private String name;
	private Category parentCategory; //  
	private Set<Category> childCategories = new HashSet<Category>(); //  

	// getter and setter
}

XMLでマッピング
<hibernate-mapping package="org.monday.hibernate4.domain">
	<class name="Category" table="tbl_category">
		<id name="id">
			<generator class="identity" />
		</id>
		<property name="name" />
		<many-to-one name="parentCategory" class="Category" column="category_id" />
		<set name="childCategories" inverse="true" cascade="all">
			<key column="category_id" />
			<one-to-many class="Category" />
		</set>
	</class>
</hibernate-mapping>

@Annotationでマッピング
 
@Entity
@Table(name = "tbl_category")
public class Category  {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	private String name;

	@ManyToOne
	@JoinColumn(name = "category_id")
	private Category parentCategory; //  

	@OneToMany(mappedBy = "parentCategory", targetEntity = Category.class, cascade = CascadeType.ALL)
	private Set<Category> childCategories = new HashSet<Category>(); //  

	//getter and setter
}

 
テストコード
                        Category foodCategory = new Category("food");
			Category fruitCategory = new Category("fruit");
			Category vegetableCategory = new Category("vegetable");
			Category appleCategory = new Category("apple");
			Category orangeCategory = new Category("orange");
			Category tomatoCategory = new Category("tomato");

			//  
			foodCategory.getChildCategories().add(fruitCategory);
			fruitCategory.setParentCategory(foodCategory);

			//  
			foodCategory.getChildCategories().add(vegetableCategory);
			vegetableCategory.setParentCategory(foodCategory);

			//  
			fruitCategory.getChildCategories().add(appleCategory);
			appleCategory.setParentCategory(fruitCategory);

			//  
			fruitCategory.getChildCategories().add(orangeCategory);
			orangeCategory.setParentCategory(fruitCategory);

			//  
			tomatoCategory.setParentCategory(vegetableCategory);
			vegetableCategory.getChildCategories().add(tomatoCategory);

			session.save(foodCategory);

 SQL schema
Hibernate: 
    create table CATEGORYS (
        id integer not null,
        name varchar(255),
        category_id bigint,
        primary key (id)
    )
Hibernate: 
    alter table CATEGORYS 
        add index FK36CF3159B3B4FFA (category_id), 
        add constraint FK36CF3159B3B4FFA 
        foreign key (category_id) 
        references CATEGORYS (id)

 
----------------------------------------------------------------------------------------------
 
1.エンティティークラスの改善
@Entity
@Table(name = "tbl_category")
public class Category {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	private String name;

	@ManyToOne
	@JoinColumn(name = "category_id")
	private Category parentCategory; //  

	@OneToMany(mappedBy = "parentCategory", targetEntity = Category.class, cascade = CascadeType.ALL)
	private Set<Category> childCategories = new HashSet<Category>(); //  

	//getter and setter

	/**   */
	public void addChildCategory(Category category) {
		if (category == null) {
			throw new IllegalArgumentException("Can't add a null Category as child.");
		}
		//  Category
		if (category.getParentCategory() != null) {
			category.getParentCategory().getChildCategories().remove(category);
		}
		//  Category
		category.setParentCategory(this);
		//  Category 
		this.getChildCategories().add(category);
	}
}

 
2.テストコード
//  
			foodCategory.addChildCategory(fruitCategory);

			//  
			foodCategory.addChildCategory(vegetableCategory);

			//  
			fruitCategory.addChildCategory(appleCategory);

			//  
			fruitCategory.addChildCategory(orangeCategory);

			//  
			vegetableCategory.addChildCategory(tomatoCategory);

			session.save(foodCategory);