-->
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: Mapping multiple rows to a single class instance
PostPosted: Fri Dec 07, 2007 12:28 pm 
Newbie

Joined: Fri Dec 07, 2007 11:35 am
Posts: 4
Suppose I have a table which stores email addresses a user id has. A user id can have several email addresses, thus multiple rows in the table

table email_user
id_index INTEGER
user_id CHAR(12)
email_addr CHAR(50)

and I have this java class representing this table
class EmailUser
{
private Integer index;
private String userId;
private Set emailAddrs; // set of Strings

...
}


how can I do this using

I've only read of using sets that contain instances of a class representing another table (ie using foreign keys) or sets containing instances of the enclosing class.

Ive tried the following but i get a jdbc prepared statement error

<class name="test.dataobjects.EmailUser" table="email_user">
<id name="index" column="id_index">
<generator class="increment" />
</id>
<property name="userId" column="user_id" />
<set name="emailAddrs">
<key column="user_id" />
<element column="email_addr" type="string" not-null="true" />
</set>
</class>

Hibernate: insert into email_user (user_id, id_index) values (?, ?)
could not bind value 'TESTSETUSER' to parameter: 1; Invalid argument: parameter index 1 is out of range.

originally the email_addr column was NOT NULL, but i changed it to accomodate the sql automatically generated above. if each row contains the same user_id value but different email addr values, why does it not include email addr in the INSERT? what is this bind value error?


using hibernate 1.3
java 1.4
websphere 6.0
DB2


Last edited by noreservations on Fri Dec 07, 2007 1:01 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Mapping multiple rows to a single class instance
PostPosted: Fri Dec 07, 2007 12:50 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
noreservations wrote:
<class name="test.dataobjects.EmailUser" table="edelivery_user">
<id name="index" column="id_index">
<generator class="increment" />
</id>
<property name="userId" column="user_id" />
<set name="emailAddrs">
<key column="user_id" />
<element column="email_addr" type="string" not-null="true" />
</set>
</class>

Hibernate: insert into user_email (user_id, id_index) values (?, ?)
could not bind value 'TESTSETUSER' to parameter: 1; Invalid argument: parameter index 1 is out of range.


I don't see user_email defined anywhere in your mapping file so it is hard to understand what that sql is. However, the element thing should work if you specify the table on the set tag, and you set a property-ref in the key tag since the primary key is not userId.

Code:
<set name="emailAddrs" table="user_email">
         <key column="user_id" property-ref="userId" />
         <element column="email_addr" type="string" not-null="true" />


Hibernate documentation has a quite similar example. You may want to check out section 1.3.4 Collection of values.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 1:11 pm 
Newbie

Joined: Fri Dec 07, 2007 11:35 am
Posts: 4
no your suggestion just makes two insert statements. it looks like hibernate thinks there are two tables

from the log:

Hibernate: insert into email_user (user_id, id_index) values (?, ?)
Hibernate: insert into emailAddrs (user_id, email_addr) values (?, ?)
could not bind value '4' to parameter: 1; Invalid argument: parameter index 1 is out of range.

the second insert completely makes no sense


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 3:31 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
noreservations wrote:
no your suggestion just makes two insert statements. it looks like hibernate thinks there are two tables

from the log:

Hibernate: insert into email_user (user_id, id_index) values (?, ?)
Hibernate: insert into emailAddrs (user_id, email_addr) values (?, ?)
could not bind value '4' to parameter: 1; Invalid argument: parameter index 1 is out of range.

the second insert completely makes no sense



Ah, now it is different. I am not sure if there is a straight forward way to do this in hibernate but you might be able to modify the way that set is manipulated by adding <sql-insert ..> <sql-update ...> <sql-delete ..> and <loader ..> tags for the set. You will also do the same for the class itself since there is no need for the class itself to be updated. There might be a way to disable updates for the class itself and not the collection. In the meantime, we can see if someone else has any other idea.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 12, 2007 5:31 pm 
Newbie

Joined: Fri Dec 07, 2007 11:35 am
Posts: 4
bump


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.