Hi
I want to construct a Hibernate object that refers to two database tables. I have one object called
Attribute and another called
Activity. The object Attribute is read only, and has a Many-to-Many relationship with Activity. The following is the definition of Attribute
Code:
<class name="Attribute" table="DAW_ATTRIBUTE_TABLE">
<id name="id" column="ATTRIBUTE_ID">
<generator class="native">
<param name="sequence">DAW_ATTRIBUTE_S</param>
</generator>
</id>
<property name="name" column="NAME"/>
<property name="description" column="DESCRIPTION" />
<set name="activities" table="DAW_USERATTRIBUTEACTIVITY_TABLE">
<key column="ATTRIBUTE_ID" />
<many-to-many column="ACTIVITY_ID" class="org.sakaiproject.dwa.api.model.Activity" />
</set>
</class>
<query name="getAttributeById">
<![CDATA[from Attribute attribute where attribute.id= :attribute]]>
</query>
<query name="getActivitiesAttributes">
<![CDATA[from Attribute as attrib inner join attrib.activities as activity where activity.userUuid= :userUuid and activity.context= :context]]>
</query>
Here is the Activity hbm.xml file
Code:
<class name="Activity" table="DAW_ACTIVITY_TABLE">
<id name="id" column="ID" length="19" unsaved-value="0">
<generator class="native">
<param name="sequence">DAW_ACTIVITY_S</param>
</generator>
</id>
<property name="userUuid" column="USER_UUID" not-null="true" />
<property name="context" column="LOCATION_ID" not-null="true" />
<property name="name" column="NAME" not-null="true" />
<property name="type" column="TYPE" not-null="true" />
<property name="importance" column="IMPORTANCE" length="1" not-null="true" />
<property name="status" column="STATUS" not-null="true" />
<property name="notes" column="NOTES" length="4000" not-null="false" />
<property name="dateCreated" column="CREATED_DATE" not-null="true" />
<property name="duration" column="DURATION" not-null="true" />
<property name="startTime" column="START_TIME"/>
<property name="editedDate" column="EDITED_DATE" not-null="false" />
</class>
My questions are
How do you (using HQL) insert AND delete a single value into the relationship table (one attribute ID, one activity ID)?
UPDATE: Considering the fact that HQL inserts only support inserts from another table, how do you reference the foreign keys in the relationship table (that is the attribute and activity identities) in HQL?
Here is my attempt -
Code:
insert into Attribute.activities (Attribute.id, Activity.id)
select attrib.id, activity.id from Attribute as attrib, Activity as activity where attrib.id= :attribute and activity.id = :activity
Error message - Attribute.activities isn't map!
Can I use a java.util.Set in the Attribute object only as a place holder and not load data into it, because I rather load the relationship in a separate object?
Do I just use <sql-query> to load the other object by specifying the bean properties of the other object?
Thanks
Patrick