-->
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: What is wrong with this mapping?
PostPosted: Mon Oct 17, 2005 11:49 am 
Newbie

Joined: Wed Oct 12, 2005 2:50 pm
Posts: 11
I have a class called Category with a list of entity objects called LinkInfo. When I remove a LinkInfo from the list, I would like the entry to be deleted from the LinkInfo table. There must be something wrong with my mappings, because when I remove an item from the list Hibernate tries to set the entry in the LINKS table to null, instead of deleting it. Here is the relevant info. Any input is much appreciated.

Code in Category to remove a LinkInfo from the list:
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.update(this);
LinkInfo li = getLink(fileId);
links.remove(li);
tx.commit();
HibernateUtil.closeSession();


Mappings:

<class name="software.executiveutilities.model.Category" table="APP.CATEGORIES" lazy="false">
<id name="id" column="ID">
<generator class="increment" />
</id>
<property name="name" column="NAME" />
<property name="textArea" column="TEXT_AREA" />

<list name="links" lazy="false" cascade="all,delete-orphan" >
<key>
<column name="CATEGORY_ID" not-null="true"/>
</key>
<list-index column="LINK_ORDER" base="0" />
<one-to-many class="software.executiveutilities.model.LinkInfo" />
</list>

</class>


<class name="software.executiveutilities.model.LinkInfo" table="APP.LINKS" lazy="false">
<composite-id name="id" class="software.executiveutilities.model.LinkInfoId" >
<key-property name="categoryId" type="int" >
<column name="CATEGORY_ID" />
</key-property>
<key-property name="fileId" type="long">
<column name="FILE_ID" />
</key-property>
</composite-id>
<property name="name" column="NAME" />

<many-to-one name="fileInfo" column="FILE_ID" not-null="true" update="false" insert="false"/>

</class>

SQL generated by Hibernate:

update APP.LINKS set CATEGORY_ID=null, LINK_ORDER=null where CATEGORY_ID=? and CATEGORY_ID=? and FILE_ID=?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 12:31 pm 
Newbie

Joined: Wed Oct 12, 2005 2:50 pm
Posts: 11
I have discovered that if I change my key element for the list in Category, to appear like this:

<key column="CATEGORY_ID" not-null="true" />

instead of:

<key>
<column name="CATEGORY_ID" not-null="true"/>
</key>


That Hibernate recognizes the not-null. I guess there is a differnce between marking the key as not-null? vs. the column being not null? Anyway, now that I have made this change I am getting a mapping error on startup:

failed.org.hibernate.MappingException: Repeated column in mapping for entity: software.executiveutilities.model.LinkInfo column: CATEGORY_ID (should be mapped with insert="false" update="false")

Problem is that I don't understand where I can set CATEGORY_ID to insert="false" update="false"? I can't do it on the key element. I am just not sure what this error is suggesting should be changed. Any help is appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 2:40 pm 
Newbie

Joined: Wed Oct 12, 2005 2:50 pm
Posts: 11
Ok this is really starting to drive me crazy now! Here are the things I have tried and what has been the result.

If I map my list in Category like this:

<list name="links" lazy="false" cascade="all,delete-orphan" >
<key not-null="true">
<column name="CATEGORY_ID" />
</key>

<list-index column="LINK_ORDER" base="0" />
<one-to-many class="software.executiveutilities.model.LinkInfo" />
</list>

I end up getting this exception: Repeated column in mapping for entity: software.executiveutilities.model.LinkInfo column: CATEGORY_ID (should be mapped with insert="false" update="false")
In this case, I think I understand what it is complaining about, but don't know what to do about it.

If I map my list like this:

<list name="links" lazy="false" cascade="all,delete-orphan" inverse="true">
<key not-null="true">
<column name="CATEGORY_ID" />
</key>

<list-index column="LINK_ORDER" base="0" />
<one-to-many class="software.executiveutilities.model.LinkInfo" />
</list>

I get no errors on startup, and I can remove entities from the list. However, when I add entities to the list the LINK_ORDER column is not updated. This behaviour is exactly what I would expect given that inverse is set to true. In any case, I wan't this to be a unidirectional association anyway, so inverse doesn't make sense.


If I map my list like this:

<list name="links" lazy="false" cascade="all,delete-orphan" >
<key>
<column name="CATEGORY_ID" />
</key>

<list-index column="LINK_ORDER" base="0" />
<one-to-many class="software.executiveutilities.model.LinkInfo" />
</list>

I can add entities to the list, but when entities are removed Hibernate attempts to set the foreign key in the LINKS table to null, and I get an SQL error. Again I think this makes sense, given the mapping.


Any help is muuuuch appreciated!! Getting desperate here.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 2:58 pm 
Regular
Regular

Joined: Mon Aug 29, 2005 9:46 am
Posts: 102
Have you tried it like this:
Code:
<list name="links" lazy="true" inverse="true">
<key column="CATEGORY_ID" not-null="true"/>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 11:01 am 
Newbie

Joined: Wed Oct 12, 2005 2:50 pm
Posts: 11
Thanks for responding moonlight.

My application design prevents me from using lazy loading for the links. However I did try this:

<list name="links" lazy="false" inverse="true">
<key column="CATEGORY_ID" not-null="true"/>
<list-index column="LINK_ORDER" base="0" />
<one-to-many class="software.executiveutilities.model.LinkInfo" />
</list>


Unfortunately with this configuration the inverse="true" means that nothing will be updated in the db as a result of changes in the list. Main problem here is that LINK_ORDER is not modified when I add a new entry to the list. Any other ideas? thx.


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.