Hi !
I have a parent/child relationship between categories, all stored in the same table. I generated the DAO using Hibernate Synchronizer. When I create a category and ask for its children, the resulting set contains the category itself instead of being empty, as expected.
Why is that ?
Thanks in advance !
Hibernate version: 3.2.1ga
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Category" table="category">
<meta attribute="sync-DAO">true</meta>
<id name="id" type="integer" column="categoryID">
<generator class="sequence" />
</id>
<property name="name" column="name" type="string"
not-null="true" length="200" />
<many-to-one name="parentID" column="parentID" class="Category"
not-null="false">
</many-to-one>
<set name="categories" inverse="true" lazy="true">
<key column="categoryID" />
<one-to-many class="Category" />
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():CategoryBLL.java
Code:
public class CategoryBLL {
/**
* Creates a new category with the given name and no parent;
* @param name The name of the category to create.
* @return The newly created category.
*/
public static Category create(String name) {
return create(name, null);
}
/**
* Creates a new category with the given name and parent.
* @param name The name of the new category.
* @param parent The parent of the new category.
* @return The newly created category
*/
public static Category create(String name, Category parent) {
if (null == name || name.equals("")) {
throw new IllegalArgumentException("name cannot be null or empty");
}
HibernateUtil.init();
CategoryDAO dao = (CategoryDAO) CategoryDAO.getInstance();
Transaction tx = dao.getSessionFactory().openSession().beginTransaction();
Category c = new Category();
c.setName(name);
c.setParentID(parent);
dao.save(c);
if (null != parent) {
addChild(parent, c);
}
tx.commit();
return c;
}
/**
* Adds a child to a parent category.
* @param parent The parent category.
* @param child The child category to add.
*/
public static void addChild(Category parent, Category child) {
if (null == parent) {
throw new IllegalArgumentException("parent cannot be null");
}
if (null == child) {
throw new IllegalArgumentException("child cannot be null");
}
if (null == parent.getCategories()) {
parent.setCategories(new HashSet<Category>());
}
parent.getCategories().add(child);
update(parent);
}
/**
* Updates a modified category.
* @param category The category to update.
*/
public static void update(Category category) {
HibernateUtil.init();
CategoryDAO dao = (CategoryDAO) CategoryDAO.getInstance();
dao.update(category);
}
/**
* Gets a category by its name.
* @param id The name of the category to get.
* @return The category with given name.
*/
public static Category getByName(String name) {
HibernateUtil.init();
CategoryDAO dao = (CategoryDAO) CategoryDAO.getInstance();
Query q = dao.getSession().createQuery("from Category where name = :name");
q.setParameter("name", name);
return (Category) q.uniqueResult();
}
}
CategoryBLLTest.java
Code:
public class CategoryBLLTest {
@Before
public void initDB() {
_RootDAO.initialize();
}
@After
public void closeSession() {
_RootDAO.closeCurrentSession();
}
@Test
public void testBugGetCategories() {
String name = "Watches";
CategoryBLL.create(name);
Category c1 = CategoryBLL.getByName(name);
Set<Category> children = c1.getCategories();
Assert.assertEquals(0, children.size());
}
}
Name and version of the database you are using: HSQLDB 1.8.0.7
The generated SQL (show_sql=true):Code:
Hibernate: select next value for hibernate_sequence from dual_hibernate_sequence
Hibernate: insert into category (name, parentID, categoryID) values (?, ?, ?)
Hibernate: select category0_.categoryID as categoryID37_, category0_.name as name37_, category0_.parentID as parentID37_ from category category0_ where category0_.name=?
Hibernate: select categories0_.categoryID as categoryID1_, categories0_.categoryID as categoryID37_0_, categories0_.name as name37_0_, categories0_.parentID as parentID37_0_ from category categories0_ where categories0_.categoryID=?