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.  [ 2 posts ] 
Author Message
 Post subject: Collection Mapping
PostPosted: Fri Nov 09, 2007 5:10 am 
Regular
Regular

Joined: Mon Oct 02, 2006 12:03 pm
Posts: 62
I have this database -->

Code:
CREATE TABLE PLACE (
  ID INT UNSIGNED NOT NULL,
  DESCRIPTION VARCHAR(64) NOT NULL,
  DEFAULT_PRINTER VARCHAR(128),
  PRIMARY KEY(ID)
);

CREATE TABLE ENTERPRISE (
  ID INT UNSIGNED NOT NULL,
  USR VARCHAR(16) NOT NULL,
  PSW VARCHAR(16) NOT NULL,
  DATA_BASE VARCHAR(32) NOT NULL,
  SERVER VARCHAR(132) NOT NULL,
  NAME VARCHAR(32) NOT NULL,
  MODEL VARCHAR(64),
  PRIMARY KEY(ID)
);

CREATE TABLE ARTICLE (
  ID INT NOT NULL,
  ENTERPRISE_ID INTEGER UNSIGNED NOT NULL,
  CODE VARCHAR(16) NULL,
  DESCRIPTION VARCHAR(64) NULL,
  STOCK INTEGER UNSIGNED NULL,
  PRIMARY KEY(ID),
  FOREIGN KEY(ENTERPRISE_ID) REFERENCES ENTERPRISE(ID)
);

CREATE TABLE STOCK_MOVEMENTS (
  MOVEMENT INTEGER UNSIGNED NOT NULL,
  TO_PLACE_ID INTEGER UNSIGNED,
  FROM_PLACE_ID INTEGER UNSIGNED ,
  MOVEMENT_TYPE VARCHAR(64),
  ARTICLE_ID INTEGER UNSIGNED NOT NULL,
  AMOUNT INTEGER UNSIGNED NULL,
  MOVEMENT_DATE DATETIME NULL,
  MOVEMENT_TARGET VARCHAR(32) NULL,
  PRIMARY KEY(MOVEMENT),
  FOREIGN KEY(ARTICLE_ID) REFERENCES ARTICLE(ID),
  FOREIGN KEY(FROM_PLACE_ID) REFERENCES PLACE(ID),
  FOREIGN KEY(TO_PLACE_ID) REFERENCES PLACE(ID)
);


I have to control the enterprise stock movements. So, the movements are saved into STOCK_MOVEMENTS table. As you can see, the movements are as --> (article, from_place, to_place, amount) basically. I have dessigned two entities --> Article and Place. I want that Article and Place entities have a movements collection that holds all movements of an article, and all movements of a place, respectly.

So, the mapping for Article.hbm.xml would be -->

Code:
<bag name="Movements" cascade="all-delete-orphan" inverse="true" lazy="false">
  <key column="ARTICLE_ID">
  <one-to-many class="Model.Entities.Movement, Model"/>
</bag>


but, I don't know How Can I map movements to Place.hbm.xml, because I need all the movements that are in ToPlace and are in FromPlace.

Code:
<bag name="Movements" cascade="all-delete-orphan" inverse="true" lazy="false">
  <key column="??????????????">
  <one-to-many class="Model.Entities.Movement, Model"/>
</bag>


I hope that you have understood what I mean.
Thanks for all.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 09, 2007 9:03 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
In this case map it as two separate collections. Having them as separate collections will actually provide you more freedom in your business objects, plus it is always possible to return a collection in code which contains all items in the from and to collections.

If you are creating a wrapper collection I actually recommend using an IEnumerable<Movement> instead of an IList or ICollection even. You could then use the built in .NET 2.0 yield function to just return all items from both collections. This will also prevent consumers from trying to add items to this "wrapper collection".

Hope that helps.


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