I failed to spot another mistake. Since you are sharing the primary key between the two tables, you need only one Id class.. ie. ProductId class only..
Here is the complete solution
Code:
package myapp;
public class Product {
private ProductId id;
public ProductId getId() {
return id;
}
public void setId(ProductId id) {
this.id = id;
}
public Translation getTranslation() {
return translation;
}
public void setTranslation(Translation translation) {
this.translation = translation;
}
private Translation translation;
}
Code:
package myapp;
public class Translation {
private ProductId id;
private String description;
public ProductId getId() {
return id;
}
public void setId(ProductId id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Code:
package myapp;
import java.io.Serializable;
public class ProductId implements Serializable {
private static final long serialVersionUID = 1L;
private String sku;
private int manuf;
private String catalog;
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public int getManuf() {
return manuf;
}
public void setManuf(int manuf) {
this.manuf = manuf;
}
public String getCatalog() {
return catalog;
}
public void setCatalog(String catalog) {
this.catalog = catalog;
}
public boolean equals(Object other) {
if (other == null)
return false;
if (!this.getClass().equals(other.getClass()))
return false;
ProductId otherId = (ProductId) other;
if (this.catalog.equals(otherId.catalog) && this.manuf == otherId.manuf
&& this.sku.equals(otherId.sku)) {
return true;
} else
return false;
}
public int hashCode() {
int result;
result = catalog.hashCode();
result = 29 * result + manuf;
result = 29 * result + sku.hashCode();
return result;
}
}
<hibernate-mapping>
<class name="myapp.Product" table="Products">
<composite-id
class="myapp.ProductId" name="id">
<key-property name="sku" type="string">
<column length="128" name="sku"/>
</key-property>
<key-property name="manuf" type="int">
<column name="manuf"/>
</key-property>
<key-property name="catalog" type="string">
<column length="128" name="catalog"/>
</key-property>
</composite-id>
<one-to-one name="translation"
constrained="true" class="myapp.Translation" cascade="save-update"/> </class>
</hibernate-mapping>
<hibernate-mapping >
<class name="myapp.Translation" table="Translations">
<composite-id
class="myapp.ProductId" name="id">
<key-property name="sku" type="string">
<column length="128" name="sku"/>
</key-property>
<key-property name="manuf" type="int">
<column name="manuf"/>
</key-property>
<key-property name="catalog" type="string">
<column length="128" name="catalog"/>
</key-property>
</composite-id>
<property name="description" column="DESC"></property>
</class>
</hibernate-mapping>
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
ProductId pid = new ProductId();
pid.setCatalog("catalog");
pid.setManuf(11);
pid.setSku("sku");
Translation translation = new Translation();
translation.setId(pid);
translation.setDescription("description");
Product testProduct = new Product();
testProduct.setId(pid);
testProduct.setTranslation(translation);
session.save(testProduct);
session.getTransaction().commit();