I am building a J2EE application around a legacy database. I was hoping I could get some input on whether or not I'm doing this "right"... I'm using Hibernate 2.1 and Oracle9.
I have three tables I'm focusing on: Supplier, Carrier and SupplierCarrier. Supplier and Carrier each have a "number" primary key plus other, additional columns. SupplierCarrier is sort of a cross-reference table with SupplierNumber and CarrierNumber foreign keys as its composite primary key. It has no primary key of its own.
I've created four classes: SupplierModel, CarrierModel, SupplierCarrierModel and SupplierCarrierModelKey. The Key class is a composite key for SupplierCarrier.
Code:
public class SupplierModel
{
private String supplierNumber;
private java.util.Set supplierCarriers;
// ...
// with default constructor, getters and setters for everything
}
public class CarrierModel
{
private String carrierNumber;
private java.util.Set supplierCarriers;
// ...
// with default constructor, getters and setters for everything
}
public class SupplierCarrierModel
{
private SupplierCarrierModelKey id;
// additional properties, getters, setters, etc.
}
public class SupplierCarrierModelKey implements java.io.Serializable
{
private SupplierModel supplier;
private CarrierModel carrier;
private ProductModel product;
public SupplierCarrierModelKey()
{
}
public SupplierCarrierModelKey(
SupplierModel supplier,
CarrierModel carrier,
ProductModel product)
{
this.supplier = supplier;
this.carrier = carrier;
this.product = product;
}
// and I implement equals and hashCode
And here is my mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="SupplierModel"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="supplierNumber"
type="java.lang.String"
>
<generator class="assigned">
</generator>
</id>
<set
name="supplierCarriers"
lazy="false"
inverse="false"
cascade="delete"
sort="unsorted"
>
<key
column="supplierNumber"
/>
<composite-element
class="SupplierCarrierModel"
>
<!-- <propertys... -->
</composite-element>
</set>
</class>
<!-- CarrierModel is similar to SupplierModel ... -->
<class
name="SupplierCarrierModel"
dynamic-update="false"
dynamic-insert="false"
>
<composite-id
name="id"
class="SupplierCarrierModelKey"
>
<key-many-to-one
name="supplier"
class="com.magellan.tas.model.SupplierModel"
column="supplierNumber"
/>
<key-many-to-one
name="carrier"
class="com.magellan.tas.model.CarrierModel"
column="carrierNumber"
/>
</composite-id>
</class>
</hibernate-mapping>
Is this right? It seems to work. But, should I be using "inverse=true"? If so, I was under the impression I needed to set an instance of the "parent" in the child... is what I'm doing with the SupplierCarrierModelKey sufficient?
Like I said, this seems to work: e.g. if I delete a supplier, it deletes the SupplierCarriers associated with it. But, am I doing this in the Right Way and the most efficient way? Thanks.
RMC