-->
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.  [ 3 posts ] 
Author Message
 Post subject: SaveOrUpdate method id generation
PostPosted: Wed Dec 12, 2007 10:39 am 
Newbie

Joined: Wed Aug 01, 2007 8:53 am
Posts: 6
Hi,

I am developping an application that synchronized some documents (.doc, .xls,.pdf) from laptops to a server, I am using Hibernate 1.2 to register the files on a SQLExpress, I make the files synchronization with an Ftp connection.

So when a user can register some files on the application on a disconnected mode, so the files are register on a directory (C:\Cleos\Data) and on a database. When the user wants to synchronized, the system verifires the status of all files in the database then throw those information on a remoting SQL Server then the files are sending to a remoting file server via the ftp connection.

Each document is an entity (DocumentNumerise) and the mapping follows :
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                            namespace="Cleos2008.Domaine.Metier" assembly="Cleos2008.Domaine">

   <class name="DocumentNumerise" table="documentNumerise" lazy="false" >
      <id name="Id" column="documentNumerise_id" type="guid">
         <generator class="guid" />
      </id>

      <property name="Ordre" column="ordre" type="integer" index="IDX_DocumentNumerise_Ordre"/>
      <property name="Publication" column="publication" type="boolean"/>
      <property name="Nom" column="nom" type="string" not-null="true" index="IDX_DocumentNumerise_Nom"/>
      <property name="Commentaires" column="commentaires" type="string" length="1000"/>
      <many-to-one name="Type" column="type" class="Cleos2008.Domaine.Aide.Referentiel" foreign-key="FK_DocumentNumerise_TypeDocument" index="IDX_DocumentNumerise_Type" />
      <many-to-one name="Bien" column="bien_id" foreign-key="FK_Bien_DocumentNumerise" index="IDX_DocumentNumerise_Bien"/>
   </class>
   <query name="FindImages">
      <![CDATA[from DocumentNumerise doc where doc.Bien = :bien and doc.Type.Libelle = 'Image' order by doc.Ordre ]]>
   </query>
   <query name="FindDocument">
      <![CDATA[from DocumentNumerise doc where doc.Bien = :bien and doc.Type.Libelle <> 'Image' order by doc.Ordre ]]>
   </query>
   <query name="FindDocumentsTelechargesByUtilisateur" >
      <![CDATA[select elements(util.DocumentTelecharge) from Utilisateur util  where util = :utilisateur]]>
   </query>
</hibernate-mapping>


When I register a file on the laptop I need to do a Save or a SaveOrUpdate to generate a new entity and genereate a new ID, but when I register the same document and if it does not exist on the server I only need to do a SaveOrUpdate and here begins the problem. In fact I can't UpDate because there is nothing to update on the server, no document exist with the actual ID and if I use the Save method this will generate an new entity with a new ID wich changes the identity of the object.

I need to keep the same ID on the laptop and on the server so the same entity, How can I resolved this problem without any hard modification?

Thx


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 13, 2007 4:13 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Build a different mapping for the server that uses an assigned ID generator. You can't use SaveOrUpdate, because it will not function in this situation, since none of the indicators is uses will function in this context. You will have to manually query for the object from the server to see if has been saved there.

The crux of the problem is the notion that you will have one entity saved in two different places. NH wasn't built for this kind of replication out-of-the-box. While it can use replicated data, an entity in two separate places is, well, two entities. I would consider using a tool designed for client-server replication to handle that part of your application, and use NH when you actually need to hydrate and use objects within your application.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 14, 2007 3:59 am 
Newbie

Joined: Wed Aug 01, 2007 8:53 am
Posts: 6
Thx marcal for your answer.

I have resolved my pb, I replaced the <generator class="guid" /> by <generator class="assigned" />, then I added a new property (that is not mapped with NHibernate) in the entity IsNew, when the entity is a new one it is = true.

Then when I want to synchronized the data between the cleint and the server I just have to test if IsNew = true, if yes I do a Save if not I do a SaveOrUpdate.

All this with the same mapping for the client and the server (so with the same dll), of course I had to do some little changes in the client code but nothing to hard.

I have also tried to implement the IInterceptor interface but strangely it does not work with my code, perhaps my HibernateTemplate is not used at the wright place, I have to investigate and I don't have too much time.

Thx


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