create table categories (
id bigint not null,
name varchar(15),
category_id bigint,
primary key (id)
);
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "model.Category" table = "Categories">
<id name = "id" column = "id" type = "long">
<generator class="increment"></generator>
</id>
<property name = "name" type = "string">
<column name="name" length="50"></column>
</property>
<set name="childCategories" cascade="all" inverse="true">
<key column="category_id"></key>
<one-to-many class="model.Category"></one-to-many>
</set>
<many-to-one name="parentCategory" column="category_id" class = "model.Category"></many-to-one>
</class>
</hibernate-mapping>
package model;
import java.util.Set;
public class Category {
private Long id;
private String name;
private Category parentCategory;
private Set<Category> childCategories;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getParentCategory() {
return parentCategory;
}
public void setParentCategory(Category parentCategory) {
this.parentCategory = parentCategory;
}
public Set<Category> getChildCategories() {
return childCategories;
}
public void setChildCategories(Set<Category> childCategories) {
this.childCategories = childCategories;
}
public Category(String name, Category parentCategory,
Set<Category> childCategories) {
super();
this.name = name;
this.parentCategory = parentCategory;
this.childCategories = childCategories;
}
public Category() {
super();
}
}
package model;
import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest2 {
private static SessionFactory sessionFactory = null;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
}catch(Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
/*
Category category1 = new Category ("level1",null,new HashSet<Category> ());
Category category2 = new Category ("level2",null,new HashSet<Category> ());
Category category3 = new Category ("level2",null,new HashSet<Category> ());
Category category4 = new Category ("level3",null,new HashSet<Category> ());
Category category5 = new Category ("level3",null,new HashSet<Category> ());
Category category6 = new Category ("level3",null,new HashSet<Category> ());
Category category7 = new Category ("level3",null,new HashSet<Category> ());
category2.setParentCategory(category1);
category3.setParentCategory(category1);
category1.getChildCategories().add(category2);
category1.getChildCategories().add(category3);
category4.setParentCategory(category2);
category5.setParentCategory(category2);
category2.getChildCategories().add(category4);
category2.getChildCategories().add(category5);
category6.setParentCategory(category3);
category7.setParentCategory(category3);
category3.getChildCategories().add(category6);
category3.getChildCategories().add(category7);
session.save(category1);
*/
Category category = (Category)session.get(Category.class, new Long(1));
System.out.println(category.getChildCategories().size());
tx.commit();
}catch(Exception ex) {
ex.printStackTrace();
if (tx != null) {
tx.rollback();
}
}finally {
session.close();
}
}
}
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select category0_.id as id0_0_, category0_.name as name0_0_, category0_.category_id as category3_0_0_ from Categories category0_ where category0_.id=?
Hibernate: select childcateg0_.category_id as category3_1_, childcateg0_.id as id1_, childcateg0_.id as id0_0_, childcateg0_.name as name0_0_, childcateg0_.category_id as category3_0_0_ from Categories childcateg0_ where childcateg0_.category_id=?
2