Code:
<class name="Website" table="WEBSITE">
<id name="id" column="ID" type="java.lang.Long">
<generator class="sequence">
.......
</generator>
</id>
not-null="true" />
<property name="name" column="NAME" type="java.lang.String"
not-null="true" />
<set name="sitemapElementSet" cascade="all-delete-orphan"
inverse="true"
where="PARENT_ID is NULL">
<key column="WEBSITE_ID" />
<one-to-many class="SitemapElement" />
</set>
<set name="menuSet" cascade="all-delete-orphan" inverse="true">
<key column="WEBSITE_ID" />
<one-to-many class="Menu" />
</set>
</class>
Code:
<class name="SitemapElement" table="SITEMAP_ELEMENT">
<id name="id" column="ID" type="long">
<generator class="sequence">
........
</generator>
</id>
<discriminator column="SITEMAP_ELEMENT_TYPE" type="string" />
<many-to-one name="website" column="WEBSITE_ID" class="Website"
not-null="true" />
<property name="name" column="NAME" type="string"
not-null="true" />
<subclass name="SitemapFolder"
discriminator-value="sm_folder">
<set name="subElementSet" cascade="all" >
<key column="PARENT_ID" />
<one-to-many class="SitemapElement" />
</set>
</subclass>
<subclass name="SitemapLink" discriminator-value="sm_link">
<set name="menuLinkSet" inverse="true" cascade="save-update">
<key column="LINK_ID" />
<one-to-many class="MenuLink" />
</set>
<property name="url" column="URL" type="java.lang.String"
not-null="true" />
</subclass>
</class>
Code:
<class name="Menu" table="MENU">
<id name="id" column="ID" type="long">
<generator class="sequence">
......
</generator>
</id>
<property name="name" column="NAME" type="string"
not-null="true" />
<many-to-one name="website" column="WEBSITE_ID" class="Website"
not-null="true" />
<set name="menuElementSet" inverse="true"
cascade="save-update" table="MENU_ELEMENT"
where="PARENT_ID is NULL">
<key column="MENU_ID" />
<one-to-many class="MenuElement" />
</set>
</class>
Code:
<class name="MenuElement" table="MENU_ELEMENT">
<id name="id" column="ID" type="long">
<generator class="sequence">
.......
</generator>
</id>
<discriminator column="MENU_ELEMENT_TYPE" type="string" />
<property name="name" column="NAME" type="string"
not-null="true" />
<many-to-one name="menu" column="MENU_ID" class="Menu"
not-null="true" cascade="save-update"/>
<subclass name="MenuFolder" discriminator-value="menufolder">
<set name="subElementSet" cascade="all" >
<key column="PARENT_ID" />
<one-to-many class="MenuElement" />
</set>
</subclass>
<subclass name="MenuLink" discriminator-value="menulink">
<many-to-one name="link" column="LINK_ID"
class="SitemapLink" not-null="true" />
<many-to-one name="layout" column="LAYOUT_ID" class="Layout"
cascade="all" />
</subclass>
</class>
It is my data schema.
I am using Spring HibernateTemplate . So my code easy
Code:
getHibernateTemplate().saveOrUpdate(newWebsite);
I am don't include java classes because they according mapping. I am using this equals and hashcode methods
Code:
public int hashCode() {
if (this.hashValue == 0) {
int result = 17;
int idValue = this.getId() == null ? 0 : this.getId().hashCode();
result = result * 37 + idValue;
this.hashValue = result;
}
return this.hashValue;
}
Code:
public boolean equals(Object rhs) {
if (rhs == null)
return false;
if (!(rhs instanceof Current_Object))
return false;
Current_object that = (Current_object) rhs;
if (this.getId() == null || that.getId() == null)
return false;
return (this.getId().equals(that.getId()));
}
equals and hashcode I am overriding for all my classes.