-->
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: how to clone objects ?
PostPosted: Mon Mar 06, 2006 5:52 pm 
Newbie

Joined: Wed Nov 02, 2005 9:52 am
Posts: 18
Location: Argentina
Hi, im developing an Questions-Answers program, having Evaluations wich have multiple Questions and this last have multiple Answers... selecting an Evaluation i wanna do a kind of "Save As.." those save the Evaluation his Q & respective Answers like new objects, any idea how to do this?

Im using NHibernate 1.0 & SQL server 2000

Thanks!!


Top
 Profile  
 
 Post subject: here´s the .hbm
PostPosted: Tue Mar 07, 2006 8:40 am 
Newbie

Joined: Wed Nov 02, 2005 9:52 am
Posts: 18
Location: Argentina
here´s the .hbm files for the upper level class

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Business.Data.Evaluations,Business" table="Evaluations">

      <id name="IdEvaluation" column="idEvaluation" type="Int32" unsaved-value="0">
         <generator class="native"/>
      </id>
      <bag name="QuestionsList" inverse="true" lazy="true" >
         <key column="idEvaluation" />
         <one-to-many class="Business.Data.Questions,Business" />
      </bag>
      <property column="title" type="String" name="title" not-null="true" length="50" />
      <property column="tDisponible" type="Int32" name="TDisponible" />
      <property column="tAviso" type="Int32" name="TAviso" />
      <property column="puntaje" type="Int32" name="Puntaje" />
      
   </class>
</hibernate-mapping>


and the Questions mapping:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Business.Data.Questions,Business" table="Questions">

      <id name="IdQuestion" column="idQuestion" type="Int32" unsaved-value="0">
         <generator class="native"/>
      </id>
      <bag name="AnswersList" inverse="true" lazy="true" >
         <key column="idQuestion" />
         <one-to-many class="Business.Data.Answers,Business" />
      </bag>
      <many-to-one name="IdEvaluation" column="idEvaluation" class="Business.Data.Evaluations,Business" />
      <many-to-one name="IdCategoria" column="idCategoria" class="Business.Data.Categorias,Business" />
      <property column="nQuestion" type="String" name="NQuestion" not-null="true" length="255" />
      <property column="activa" type="Boolean" name="Activa" not-null="true" />
      
   </class>
</hibernate-mapping>


here's the mapping for the Answers

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Business.Data.Answers,Business" table="Answers">

      <id name="IdAnswer" column="idAnswer" type="Int32" unsaved-value="0">
         <generator class="native"/>
      </id>
      <many-to-one name="IdQuestion" column="idQuestion" class="Business.Data.Questions,Business" />
      <property column="nAnswer" type="String" name="NAnswer" not-null="true" length="255" />
      <property column="valid" type="Boolean" name="Valid" not-null="true" />
      
   </class>
</hibernate-mapping>


i need to "Save as..." (clone except by the id) an upper lever class Evaluations and with this all the related Questions and all the Answers for these related questions.

Tks again!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 3:10 pm 
Contributor
Contributor

Joined: Sun Jun 26, 2005 5:03 am
Posts: 51
Location: London, UK
Cloning is fundamentally a domain question, not a persistence one, since you may want to do a "shallow" (copy values but keep the same entity references) or "deep" (create new entities to refer to).

Implement Clone methods e.g. (CloneShallow, CloneDeep) on your low-level classes and then either have a factory class to do the appropriate level of cloning.

One you have your new top-level class, just call saving the normal manner and it will persist it. NB When you clone entities, make sure that you initialize any identity properties so that NHibernate can tell it's a new object.

_________________
Paul Hatcher
NHibernate Team


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.