I have a question about parent/child mapping. I'm being guided in the 'correct' or 'elegant' way to do this by the Hibernate Docs:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/example-parentchild.htmlIt seems I'm being forced to deviate from a POJO design I would normally use, in order to implement Hibernate cascading deletes for a parent/child relationship. Maybe someone could help and tell me if I'm understanding it wrong, or that's just the way it is!
To illustrate I'm using a parent / child relationship with real world familiar object names. I have a Year object that contains a List of Month object(s). If I delete the Year, I would expect a cascading delete of the associated Months.
It looks like I'm being forced to add a property to the Month object that I wouldn't normally have, to represent the foreign key associating the Month to the parent Year object. This is then mapped as a <many-to-one> to the Year that 'owns' the Month. In a POJO scenario, the Month is contained in the Year's Month collection and that's all it needs to know. Can someone tell me if I've missed the point, or is this something I have to accept.
I hope the pseudo code and actual mapping below help explain... and I appreciate any suggestions. I've also put an XML note where I have a problem in the mapping.
Here's the jist of my Year object:public class MyYear {
private Integer yearID; //from DB sequence
private Integer yearNumber; //actual year e.g. 2009
private List<myMonth> myMonths =new ArrayList<MyMonth>();
Here's the jist of my Month object:public class MyMonth {
private Integer monthID; //from DB sequence
private Integer monthNumber; //e.g. 7 = July
Here's my Year object mapping:<?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="com.hibernate.MyYear" schema="public" table="my_year">
<id name="yearID" type="java.lang.Integer">
<column name="year_id" not-null="true"/>
<generator class="native"/>
</id>
<property name="yearNumber" type="java.lang.Integer">
<column name="year_number"/>
</property>
<bag
name="myMonths"
order-by="month_number"
table="my_month"
cascade="all"
inverse="true">
<key column="year_id"/>
<one-to-many class="com.hibernate.myMonth"/>
</bag>
</class>
</hibernate-mapping>
Here's my Month object mapping:<?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="com.hibernate.MyMonth" schema="public" table="my_month">
<id name="monthID" type="java.lang.Integer">
<column name="month_id" not-null="true"/>
<generator class="native"/>
</id>
<property name="monthNumber" type="java.lang.Integer">
<column name="month_number"/>
</property>
<!--PROBLEM HERE... I don't want a property on the Month, that refers to the Year-->
<many-to-one name="parent" column="parent_id" not-null="true"/>
</class>
</hibernate-mapping>