Oct 13, 2010

f Comment

Hibernate Self Join - Create Hibernate Configuration

MenuHow to Do Self Join Many-To-Many Mapping In Hibernate
Hibernate Self Join - Create DB Tables
Hibernate Self Join - Create Java Class
Hibernate Self Join - Create Hibernate Configuration
Hibernate Self Join - Test Java Code
Amazon Now that we have database tables and Java class down, we have to tell Hibernate that we are doing self joins on Keyword. We can do this with XML configuration or Annotation. Here's the XML configuration. You can easily adapt it to Annotation if you are using Annotation. Questions?
<?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 default-lazy="false">
  <class name="entity.Keyword" table="keyword">
    <id name="keywordId" column="keyword_id">
    <generator class="increment" />
    <property name="title" column="title" />
    <property name="body" column="body" />
    <property name="lastModifiedTime" column="last_modified_time" />
    <property name="createTime" column="create_time" />
    <set name="parents" table="keyword_to_keyword" cascade="none" lazy="false">
      <key column="child_id"/>
      <many-to-many column="parent_id" class="entity.Keyword" />
    <set name="children" table="keyword_to_keyword" cascade="none" lazy="false">
      <key column="parent_id"/>
      <many-to-many column="child_id" class="entity.Keyword" />
We simply tell Hibernate that keyword_to_keyword has a column 'parent_id' that refers to another Keyword, joined by keyword_id column, as the parent. and it has a column 'child_id' that refers to another Keyword as the child. Since a keyword can have many parents and/or children this is a many-to-many mapping! Questions? Let me know!

◀ Create Java ClassTest Java Code ▶
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael