-->
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.  [ 8 posts ] 
Author Message
 Post subject: List Index not saved with Hibernate 3.1
PostPosted: Mon Apr 24, 2006 8:39 pm 
Newbie

Joined: Thu Apr 06, 2006 6:08 pm
Posts: 1
I am having a problem creating a simple one-to-many association using List. I've simplified the problem down to the basics. I have 2 objects Product and Image. Products can have a list of images. When I insert a new product containing a List of images (in this example, only 1), the Product and Image rows are inserted into the DB but the Index on the Image is null. I've checked the documentation, faq and forums for possible "newbie" errors and am stuck! Any help is appreciated.

The problems is the same with MySQL 5 and Oracle 10 XE.

I've included the mapping files, java class files and SQL DDL for MySQL below. The trivial code that creates the objects and calls hibernate
looks like this:
session = hibernateUtil.getSessionFactory().getCurrentSession();
tx=session.beginTransaction();

Product p = new Product("New PRODUCT short description", null);
// Create a couple of image definitions;
Image i1 = new Image("Description of image 1.");
p.addImage(i1);
try {

// Save the object
session.save(p);
tx.commit();
} catch (Exception e) {
System.out.println("Error saving Product:" + e);
}



Hibernate version:3.1

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="webstore.Product" table="PRODUCT2">
<id name="productID" column="PRODUCT_ID">
<generator class="native"/>
</id>
<property name="shortDesc" column="SHORT_DESC"/>
<list name="images" inverse="true" cascade="all-delete-orphan">
<key > <column name="PRODUCT_ID_FK" not-null="true"/>
</key>
<index column='IDX'/>
<one-to-many class="webstore.Image"/>
</list>
</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="webstore.Image" table="IMAGE2">
<id name="imageID" column="IMAGE_ID">
<generator class="native"/>
</id>
<property name="desc" column="DESCRIPTION"/>
<many-to-one name="product" column="PRODUCT_ID_FK" not-null="true"/>
</class>
</hibernate-mapping>


The Product Class file:

package webstore;

import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;

/**
* Encapsulates an Order Item in our object model
*/
public class Product
{

private int productID; // Primary Key
private String shortDesc; // Short description of product
private List images; // Images associated with this product

/* Constructors */
public Product() {
this.images = new ArrayList();
}

public Product(String shortDesc, List images) {
this.shortDesc = shortDesc;
this.images=images;
if (images==null)
this.images = new ArrayList();
}

/* Setters and Getters */

public int getProductID() {
return productID;
}
public void setProductID(int productID) {
this.productID = productID;
}

public String getShortDesc() {
return shortDesc;
}
public void setShortDesc(String shortDesc) {
this.shortDesc = shortDesc;
}

public List getImages() {
return images;
}
public void setImages(List images) {
this.images = images;
}
/**
* Get the Product as a String
*/

public String toString()
{
return "ProductID :" +productID +" ShortDesc :" +shortDesc +
" Images :[" +images.toString() + "]" ;
}

/*
* addChild adds an image to the List and sets the product property in the image
* to us.
*/
public void addImage(Image i) {
i.setProduct(this);
images.add(i);
}

}

The Image Class file:
package webstore;


public class Image {
// Private data
private int imageID;
private String desc; // Image Description
private Product product; // Owning parent (for Hibernate)

// Default constructor
public Image() { }

// Constructor with all parms

// Constructor witho ImageID
public Image(String desc) {
this.desc=desc;
}

public String getDesc() {
return desc;
}

public int getImageID() {
return imageID;
}

// Hibernate needs this for child/parent relations
public Product getProduct() {
return product;
}

public void setDesc(String desc) {
this.desc = desc;
}

public void setImageID(int imageID) {
this.imageID = imageID;
}

// Hibernate needs this for child/parent relations
public void setProduct(Product product) {
this.product = product;
}

}

MySQL DDL looks like this:

CREATE TABLE PRODUCT2 ( PRODUCT_ID INT NOT NULL AUTO_INCREMENT, SHORT_DESC VARCHAR(50), PRIMARY KEY(PRODUCT_ID) ) ENGINE=InnoDB;


CREATE TABLE IMAGE2 ( IMAGE_ID int NOT NULL AUTO_INCREMENT, DESCRIPTION VARCHAR(50), PRODUCT_ID_FK INT, IDX INT, PRIMARY KEY (IMAGE_ID),
FOREIGN KEY (PRODUCT_ID_FK) REFERENCES PRODUCT2(PRODUCT_ID) ) ENGINE=InnoDB;


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 1:05 pm 
Newbie

Joined: Tue Dec 05, 2006 1:02 pm
Posts: 7
Did you ever figure this out? I'm having the same issue. My List mapping looks just like in the documentation, yet my list-index column is empty in the table.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 1:30 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Add idx field to the Image pojo with appropriate setter and getter.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 9:54 am 
Senior
Senior

Joined: Tue Aug 03, 2004 2:11 pm
Posts: 142
Location: Somerset
Ananasi wrote:
Add idx field to the Image pojo with appropriate setter and getter.


Do you need this ? There is nothing in the hibernate mapping that links an index column to an index field on a pojo.

I thought the index column on the database was just a handy way of persisting the position of an element in a list, so that when Hibernate hydrates the list, it comes back in the right order.

_________________
On the information super B road


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 2:08 pm 
Newbie

Joined: Mon Aug 14, 2006 9:22 am
Posts: 4
did anyone get anywhere on this ? I too am getting the same problem and have tried the idx getter/setter - no joy still


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 10:25 am 
Newbie

Joined: Mon Aug 14, 2006 9:22 am
Posts: 4
I eventually sorted this out by removing the 'inverse="true"' setting. Let me know if this helps anyone else


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 10:28 am 
Newbie

Joined: Tue Sep 26, 2006 8:34 am
Posts: 16
Location: Paris, France
check my thread, there is plenty of reference etc

http://forum.hibernate.org/viewtopic.php?t=968296

but still do not work for me, please let me know if you solve it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 12:24 pm 
Newbie

Joined: Tue Sep 26, 2006 8:34 am
Posts: 16
Location: Paris, France
i finally solve my pb..

check http://forum.hibernate.org/viewtopic.php?t=968296


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.