-->
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.  [ 5 posts ] 
Author Message
 Post subject: one-to-many mapping--delte from collection with bag
PostPosted: Mon Aug 13, 2007 12:06 pm 
Newbie

Joined: Mon Aug 13, 2007 11:54 am
Posts: 6
Hibernate version: 3.x

Code:
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="com.tylernet.smartwerks.common.hibernate.objects.inventory.InventoryItem" table="inv_master" schema="tyler">
        <id name="itemID" type="string">
            <column name="itemnum" length="64" />
            <generator class="assigned" />
        </id>
        <bag name="aliases" lazy="false" inverse="true" cascade="all">
            <key column="item_num"/>
            <one-to-many class="com.tylernet.smartwerks.common.hibernate.objects.inventory.Aliases"/>
       </bag>
         </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 package="com.tylernet.smartwerks.common.hibernate.objects.inventory">
  <class name="Aliases" table="inv_alias" schema="tyler">
      
      <id name="inv_item_alias_key" type="int">
         <generator class="sequence">
            <param name="sequence">tyler.inv_alias_inv_item_alias_key_seq</param>
         </generator>
      </id>
      
      <property name="itemNumber" type="string" column="item_num" />
      <property name="alias" type="string" column="alias" />
      
   </class>
</hibernate-mapping>



Name and version of the database you are using: PostgreSQL

I have a parent child one-to-many relationship with fk keys in alias table pointing to the item table. My mappings work correctly on all types of crud except when I try and delete one or more items in the collection of items many aliases. When I procede with my update to the database my alias that I deleted from the collection does not delete them in the databse. It throws no exception and no errors, am I missing something?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 1:01 pm 
Beginner
Beginner

Joined: Sat Jan 14, 2006 10:05 am
Posts: 22
Location: spb.ru
You've got a very strange mapping.

First, you should choose which association you want: unidirection or bidirectional?

1) if you want unidirectional association remove inverse="true" from InventoryItem.aliases. and remove Aliases.itemNumber

2) ... bidirectional: map item_num as property of InventoryItem and update them accordingly with state of InventoryItem.aliases collecton


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 13, 2007 1:36 pm 
Newbie

Joined: Mon Aug 13, 2007 11:54 am
Posts: 6
expp wrote:
You've got a very strange mapping.

First, you should choose which association you want: unidirection or bidirectional?

1) if you want unidirectional association remove inverse="true" from InventoryItem.aliases. and remove Aliases.itemNumber

2) ... bidirectional: map item_num as property of InventoryItem and update them accordingly with state of InventoryItem.aliases collecton


I am new to hibernate so be gentle. First im not sure which one I need uni or bi directional. I really just need it so that when I read an item my item object that has an alias list get's filled with all alias that have the item-num that matches the item I'm calling so I assume I dont need bi-directional association.

Second... my senario would be to get Item item.item_num="4ckdr" and the mapping then gets all the item.aliases that have item_num="4ckdr". If I then take and add an item to the collection aliases and save the item with saveOrUpdate(item) it works fine, same when I change one of an items aliases on the collection. But if I delete and Item on the aliases collection and then saveOrUpdate(item) it doesnt delete that item from the database. It seems like this should work this way. Am I wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 3:01 am 
Beginner
Beginner

Joined: Sat Jan 14, 2006 10:05 am
Posts: 22
Location: spb.ru
instead
Code:
<property name="itemNumber" type="string" column="item_num" />

put
Code:
<many-to-one
name="item"
column="item_num"
class="InventoryItem"
not-null="true"/>


declare reference

Code:
class Aliases{
  InventoryItem item;
}


synchronize changes in the both sides of association

Code:
alias.setItem(theItem);
theItem.getAliases().add(alias);


and

Code:
alias.setItem(null);
theItem.getAliases().remove(alias);
session.remove(alias);


Top
 Profile  
 
 Post subject: Constraints Violation Exception?
PostPosted: Wed Aug 15, 2007 1:16 pm 
Newbie

Joined: Mon Aug 13, 2007 11:54 am
Posts: 6
alias.setItem(null);
theItem.getAliases().remove(alias);

------->session.remove(alias);

why cant I just do session.saveOrUpdate(someItem);

its works for everthing else(save, update) I dont want to remove them from the database right when I remove them from the object nor do I want to even have the session open. I just want to be able to
saveOrUpdate(anItem);

This work for saves and update and for full delete(if I delete an item it will delete all of the items aliases too). but if I try and remove one alias from the collect in an item it set's the foriegn key in alias to null and try's to save it to the database(dereferencing instead of deleting it from the database). Why then wouldn't hibernate just instead delete an object from the database when it comes along to save the items collection of alias's. I get the contrainst violation and have to set the foriegn key to not null. I also dont want to have to keep track of wants been removed from the collection and then do session.remove(alias); for each on I remove on top of dooing saveOrUpdate(item). Is this possible to do?


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