I have problem here of converting a <set> collection into a more appropriate
<map>.
I'd lilke to convert MemberBase.fields to a map of [field name -->
MemberBaseField objects],
such that I could do something like this easily...
MemberBase mb = session.load(....)
MemberBaseField field = (MemberBaseField)mb.getFields().get("name");
As a set, looking up for a particular MemberBaseField is
difficult...something like this.
MemberBase mb = session.load(....)
MemberBaseField field = null;
Iterator it = mb.getFields();
while (it.hasNext()) {
field = (MemberBaseField)it.next();
if (field.getName().equals("name")) break;
else field=null;
}
But the problem is if I use map, then the Key would have to be
the field name and unfortunately this field name may be used by
other memberbase records as well. (See example data at the end)
One solution I could think of is a composite key of memberbase+fieldname.
But I'd like to lookup the map based on a simple fieldname key alone.
Anybody with an elegant solution?
Thanks!
Regards
Kenneth
######################################
CLASSES
######################################
//--------------------------------------------------------------------------
-
// This class models a database table
// The attribute "fields" holds a set of MemberBaseField objects.
// I want to convert this set instead, to a map of
// field name to MemberBaseField objects.
//
// How do I do so?
//--------------------------------------------------------------------------
-
public class MemberBase implements Serializable {
private int id; //Database ID
private String name; //Name of database
private Set fields; //Set of fields in database. HOW TO CONVERT THIS TO
MAP?
//getters and setters here ....
}
public class MemberBaseField implements Serializable {
private String name; //Field name
private int type; //Field SQL type
private int role; //Field role
//getters and setters here
}
######################################
MAPPING
######################################
<hibernate-mapping>
<class name="com.nic.interx.domain.MemberBase" table="MEMBERBASES">
<id name="id" column="ID" type="integer">
<generator class="sequence">
<param name="sequence">GEN_MEMBERBASE</param>
</generator>
</id>
<property name="name" column="NAME" type="string"/>
<set name="fields" table="MEMBERBASE_FIELDS">
<key column="MEMBERBASE_ID"/>
<composite-element class="com.nic.interx.domain.MemberBaseField">
<property name="name" column="FIELD_NAME" type="string"/>
<property name="type" column="FIELD_TYPE" type="short"/>
<property name="role" column="FIELD_ROLE" type="short"/>
</composite-element>
</set>
</class>
</hibernate-mapping>
######################################
SQL
######################################
CREATE TABLE MEMBERBASES (
ID INTEGER NOT NULL PRIMARY KEY,
NAME VARCHAR(100),
);
CREATE TABLE MEMBERBASE_FIELDS (
MEMBERBASE_ID INTEGER NOT NULL,
FIELD_NAME VARCHAR(40) NOT NULL,
FIELD_TYPE SMALLINT NOT NULL,
FIELD_ROLE SMALLINT NOT NULL
);
CREATE INDEX IDX_MBFIELDS_1 ON
MEMBERBASE_FIELDS(MEMBERBASE_ID,FIELD_NAME,FIELD_TYPE,FIELD_ROLE);
//Sample code
INSERT INTO MEMBERBASES (ID) VALUES (0);
INSERT INTO MEMBERBASES (ID) VALUES (1);
INSERT INTO MEMBERBASE_FIELDS
(MEMBERBASE_ID,FIELD_NAME,FIELD_TYPE,FIELD_ROLE) VALUES (0,'email',1,0);
INSERT INTO MEMBERBASE_FIELDS
(MEMBERBASE_ID,FIELD_NAME,FIELD_TYPE,FIELD_ROLE) VALUES (0,'name',1,0);
INSERT INTO MEMBERBASE_FIELDS
(MEMBERBASE_ID,FIELD_NAME,FIELD_TYPE,FIELD_ROLE) VALUES (0,'address',1,0);
INSERT INTO MEMBERBASE_FIELDS
(MEMBERBASE_ID,FIELD_NAME,FIELD_TYPE,FIELD_ROLE) VALUES (1,'email',1,0);
INSERT INTO MEMBERBASE_FIELDS
(MEMBERBASE_ID,FIELD_NAME,FIELD_TYPE,FIELD_ROLE) VALUES (1,'name',1,0);
INSERT INTO MEMBERBASE_FIELDS
(MEMBERBASE_ID,FIELD_NAME,FIELD_TYPE,FIELD_ROLE) VALUES (1,'address',1,0);
-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills. Sign up for IBM's
Free Linux Tutorials. Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel