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.  [ 4 posts ] 
Author Message
 Post subject: NonUniqueObjectException
PostPosted: Tue Feb 03, 2004 2:59 pm 
Regular
Regular

Joined: Wed Nov 26, 2003 6:22 am
Posts: 76
Location: Stockholm
Class InfoContact has the following mapping.


Code:
<hibernate-mapping>
<class
name="ks.rah.cainfo.db.model.InfoContact"
table="CONTACT"
dynamic-update="false"
dynamic-insert="false"
>
.
.
.
<list
name="data"
table="DATA"
lazy="false"
inverse="false"
cascade="save-update"
>

<key
column="CONTACT_FK"
/>

<index
column="INDEX_"
/>

<composite-element
class="ks.rah.cainfo.db.model.FieldValue"
>

<many-to-one
name="field"
class="ks.rah.cainfo.db.model.ContactField"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
column="FIELD_FK"
/>

<many-to-one
name="value"
class="ks.rah.cainfo.db.model.ContactMeta"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
column="VALUE_FK"
/>

</composite-element>

</list>
.
.
</class>


I would want the InfoContact-object to have a List of
FieldValue-object where the 'field' part isn't necessarily unique,
for example:

InfoContact:data
[field1; value1]
[field1; value2]
[field2; value7]
etc.

Problem in short is this:

When I execute the following code (JUnit-test)

[code]public void testCreateNew() throws HibernateException
{
InfoContactDAO infomgr = new InfoContactDAO();
ContactMetaDAO metamgr = new ContactMetaDAO();
ContactFieldDAO fieldmgr = new ContactFieldDAO();
InfoContact contact = new InfoContact();
ContactField gender = fieldmgr.retrieve("K


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 4:57 pm 
Regular
Regular

Joined: Wed Dec 31, 2003 4:26 am
Posts: 108
Location: Berkeley, CA
Well its a rather lengthy post, so shortening it would help.

It seems that rather than having a component inside a class you have a List of components. The former will map to the class's table and be identified by its owning class. The latter will map to a foreign table (unless your DB supports objects in the the DB) and will need to have a unique key.

I suggest changing the mapping. do not use a nested component, use a separate class and a separate mapping for it. You can unique the object off of its index (though this will pose a problem if you ever need to change the list of items in the owning/main class). Instead of a List you probably want to use a Map for this case (so the keys can be arbitrary).

In short if you have multiple instances of a class (i.e., not a simple nested component) you must define a key property for that class. That key must not change for the lifetime of the instance. A single nested component is more like a custom user type -- it is a value object vis-a-vis its containing class. This nicety is lost as soon as you have a one-to-many relationship with your components.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 2:37 am 
Regular
Regular

Joined: Wed Nov 26, 2003 6:22 am
Posts: 76
Location: Stockholm
Hi, thanks for answering!
eepstein wrote:
Well its a rather lengthy post, so shortening it would help. .[/quote

Yes, I'm aware of that, but I'm still a newbie. Hard to distinguish relevant info. Sorry.

Quote:
I suggest changing the mapping. do not use a nested component, use a separate class and a separate mapping for it.


I've tried that approach, even implemented equals and hashCode for the ternary association object (which were contained as List in the InfoContact object) but ran into numerous NPE:s when trying to manipulate the collection.

Somehow I fixed the above problem by turning cascade="save-update" into cascade="none". I still don't know why it works exactly.
A theory is that the ContactMeta object, which is loaded before the InfoContact containing FieldValue-collection, chokes when InfoContactDAO.create() tries to saveOrUpdate the ContactMeta object involved.

Sincerely,

/F


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 4:23 am 
Regular
Regular

Joined: Wed Dec 31, 2003 4:26 am
Posts: 108
Location: Berkeley, CA
I also then went and re-read some of the relevant parts of the reference docs (chapters 7, I believe). And discovered to my embarassment, that much of what I said is wrong. Hibernate is very flexible... Anyway, glad you got it working.


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