-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Child-Parent mapping error
PostPosted: Wed Feb 23, 2005 10:54 pm 
Newbie

Joined: Wed Feb 23, 2005 10:36 pm
Posts: 7
Location: Taipei
Hi guys,

We have an old database running in MySQL. I want to Am pulling my hair with this error:

net.sf.hibernate.MappingException: composite-id class must override equals(): ontvmall.com.tw.hibernate.HProducts

The DB looks like this (PRODUCT_IMAGES PK is a child):

CREATE TABLE PRODUCT_IMAGES(
Product_ID INT NOT NULL,
Icon BLOB NOT NULL,
Icon_Name VARCHAR(20) NOT NULL,
VOD_Background BLOB NOT NULL,
VOD_Background_Name VARCHAR(20),
VOD_Substitute BLOB,
VOD_Substitute_Name VARCHAR(20),
More_Info BLOB,
More_Info_Name VARCHAR(20),
PRIMARY KEY (Product_ID)
)TYPE=INNODB;


CREATE TABLE PRODUCTS(
Product_ID INT NOT NULL,
Product_Code VARCHAR(15) NOT NULL,
Long_Name VARCHAR(50),
Short_Name VARCHAR(10),
VOD_Parameter VARCHAR(50),
Product_Count INT,
Initial_Price DOUBLE NOT NULL,
Minimum_Bid DOUBLE,
End_Date DATETIME NOT NULL,
Sell_Price DOUBLE,
PRIMARY KEY (Product_ID)
)TYPE=INNODB;


and the classes as follows:

public class HProductImages{
private Integer id;
private Blob icon;
private Blob vodBackground;
private Blob vodSubstitute;
private Blob info;
private String iconName;
private String vodBackgroundName;
private String vodSubstituteName;
private String infoName;
private HProducts product;
}

public class HProducts implements Serializable{
private Integer id;
private String productCode;
private String longName;
private String shortName;
private String vodParameter;
private Integer productCount;
private Double initialPrice;
private Double minimumBid;
private Date endDate;
private Double sellPrice;
}


And HProductImages.hbm.xml has this:
<class name="HProductImages" table="PRODUCT_IMAGES">
<composite-id name="product" class="HProducts" >
<key-property name="id" column="Product_ID" type="java.lang.Integer"/>
</composite-id>
<!-- property goes here -->
...
</class>


Can anyone please shed light on what this error is?

Thank you.
[/quote]

_________________
The button for mass destruction


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 24, 2005 3:48 am 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
Why not just use the PK id for HProductImages, instead of that composite-id?

The error "composite-id class must override equals()" hints pretty strongly that you need to override equals for that class, if you use a composite-id. Did you try that?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 24, 2005 4:28 am 
Newbie

Joined: Wed Feb 23, 2005 10:36 pm
Posts: 7
Location: Taipei
Well here are the reasons:
1. the key for PRODUCT_IMAGE isnt auto-generated. Instead the key is a Foreign key from PRODUCTS table's Primary Key. In every entry of the PRODUCT table, there is an entry in the PRODUCT_IMAGE.
2. True I could create an ordinary mapping and have the PK defined by me, but that wont be a good design. I want to maintain relationship.

I just want to know if composite-id is the correct way to map this. If it is, then i would like to know what's wrong with it.

Thank you.

_________________
The button for mass destruction


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 24, 2005 10:27 am 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
kungfujava wrote:
Well here are the reasons:
1. the key for PRODUCT_IMAGE isnt auto-generated. Instead the key is a Foreign key from PRODUCTS table's Primary Key. In every entry of the PRODUCT table, there is an entry in the PRODUCT_IMAGE.

As the error states, you'll need to override equals() to take this into account if you do this.
kungfujava wrote:
2. True I could create an ordinary mapping and have the PK defined by me, but that wont be a good design. I want to maintain relationship.

I still don't think composite id is correct for this case. The id is not a method for maintaining a relationship, but for assigning tuple identity. Use a constraint to seal the relationship.
Sounds like a standard one-to-one property instead:
http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-onetoone


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 12:54 am 
Newbie

Joined: Wed Feb 23, 2005 10:36 pm
Posts: 7
Location: Taipei
Thanks man it works! May I post this for other people's reference

For Product Class:


public class Products implements Serializable{
private java.lang.Integer productId;
private ProductImages productImages;
private java.lang.Double sellPrice;
//etc
}


<class name="Products" table="PRODUCTS">
<id name="productId" column="Product_ID" type="java.lang.Integer">
<generator class="increment"/>
</id>
<one-to-one name="productImages" class="ProductImages" cascade="save-update"/>
...all property here
</class>

For ProductImages Class:

public class ProductImages {

private Products products;
private Integer productId;
private String iconName;

<class name="ProductImages" table="PRODUCT_IMAGES">
<id name="productId" column="Product_ID" type="java.lang.Integer">
<generator class="foreign">
<param name="property">products</param>
</generator>
</id>

<one-to-one name="products" class="Products" constrained="true"/>
...all property here
</class>


_________________
The button for mass destruction


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.