I want to update a little on the problem we are having. It is still not resolved so any help would be much appreciated:

This all has to do with saving images to our database (as Base64 Strings) in a class called ImageVO which hold image, title, tags, etc...

We have a contacts class (ContactVO) which holds two things related to images: 1) an defaultImage & 2) an images collection

OUR JAVA CLASS
    @ManyToOne
    public ImageVO getDefaultImage() {
        return defaultImage;
    }

    public void setDefaultImage(ImageVO defaultImage) {
        this.defaultImage = defaultImage;
    }
    @ManyToMany(cascade = { CascadeType.ALL })
    public Set<ImageVO> getImages() {
        return images;
    }

    public void setImages(Set<ImageVO> images) {
        this.images = images;
    }

OUR OUR ACTIONSCRIPT CLASS

  [ArrayElementType("com.onefoot.dbocl.vo.misc.ImageVO")]
  public var images : ArrayCollection = new ArrayCollection();

  public var defaultImage : ImageVO;

OUR DESTINATION
(** notice we do not set up a separate destination for Images since we have no need to manage a separate list of all our images in the database. However, do we need a destination for every potential nested collection in our objects? I hope not, but possibly this is the case.)

<destination id="contacts">
   <adapter ref="java-dao" />
    <properties>
     <use-transactions>true</use-transactions>
<source>flex.data.assemblers.HibernateAnnotationsAssembler</ source>
     <scope>application</scope>
     <metadata>
      <identity property="id"/>
      <many-to-many property="addresses" destination="addresses"/>
     </metadata>
     <network>
      <session-timeout>20</session-timeout>
      <paging enabled="false" pageSize="10" />
      <throttle-inbound policy="ERROR" max-frequency="500"/>
      <throttle-outbound policy="REPLACE" max-frequency="500"/>
     </network>
     <server>
<hibernate-entity>com.onefoot.dbocl.vo.contacts.ContactVO</ hibernate-entity>
      <fill-method>
       <name>fill</name>
       <params>java.util.List</params>
      </fill-method>
      <fill-configuration>
       <use-query-cache>false</use-query-cache>
       <allow-hql-queries>true</allow-hql-queries>
      </fill-configuration>
     </server>
    </properties>
 </destination>

OUR CODE
In our code we create an ImageVO (myNewImageVO using our bitmap to Base64 conversion) and then add that image to the contact's image list, we then want to add the same image as the default image.

contactVO.images.addItem(myNewImageVO);
contactVO.defaultImage = myNewImageVO;
//WE TRIED THIS AS WELL WITH NO SUCCESS...
//contactVO.defaultImage = contactVO.images.getItemAt(0) as ImageVO;

HERE IS WHAT IS HAPPENING
1) with the "addItem" call (line 1), Flex-Hibernate is adding the image row to the images table & adding an entry in the contacts_images table. What we would expect!

2) when we then add the same image to the defaultImage property (line 2) it adds it again to the database in a new row and then puts the key of that row in a the defaultImage_id column in contacts. Basically it seems it is not seeing that the image is the same image as the one added to the images collection...not desirable...

3) HOWEVER, here is what is strange...
It then updates the row in the contacts_images table with the new id of this second image it has added, thus linking them all together like they should be. So ultimately, we have the structure we are after...but unfortunately, it doesn't remove the old image so now we have an orphaned image in the database.

(It is important to note that this same behavior happens when we make the two calls consecutive OR when we call them separately. It is also important to note that doing this same process directly in Java (with Hibernate) works correctly without the duplicate image creation, so we are lead to believe there is something in the way we have set up Flex to work with Java & Hibernate that is causing this to behave this way. Obviously, doing another complete insert of the image is wasteful since all that really needs to happen when one updates the defaultImage property is for a "id" to get saved in the contacts table...

If you have made it this far, thanks for reading and hopefully you can help us find out where we are going wrong with this stuff. We have over 100 classes most of which include nested collections so this is going to be of great importance to us as we are developing. Also, if there is any documentation (other than the dev guide which is nice but seems incomplete) that give a more detailed description of each of the possible parameters in the data-management-config.xml, that would really help. Are there docs for this? We are finding it very hard to understand what to do in that document to get the behavior we are looking for. For example, what are the possible properties that can go inside the metadata tag?...

Thanks for your help with this!

- Kevin





On Nov 7, 2007, at 10:10 PM, Kevin wrote:

I am having a problem where instead of just saving a reference to a class, HIbernate is creating a two database entries with the same information. Here is a quick outline of the problem:


I have a contact class & image class
The contact class hold the following image info:

public var defaultImage : ImageVO;

[ArrayElementType("com.onefoot.dbocl.vo.misc.ImageVO")]
public var images : ArrayCollection = new ArrayCollection();

Thus one can store an unlimited amount of images for each contact. And assign one of those images as the default image.

I have dataservices set up to automatically manage my contacts collection through Hibernate... thus when I add

contactVO.defaultImage = someImageVO;

the image gets added to the images table in mysql.
an entry gets added to the contacts_images table.
what you would expect...

However if I do the following:
contactVO.defaultImage = someImageVO;
contactVO.images.addItem(contactVO.defaultImage);

Then two entries get added to the images table. basically the image gets duplicated in the table instead of just adding the default image id to the contacts table...

Any thoughts on how I should be approaching this differently to get this to work. I realize all to well that these set ups are hard to describe so if you need more information please ask.

Thanks for your help.

- Kevin



Reply via email to