Hey guys,
I have an association table which has a surrogate key (a primary key per se) and am trying to use IDBAG to insert the data in the association table.
TABLE: BUDGETTOBUDGETCATEGORY
BUDGETTOBUDGETCATEGORYID (PK) BUDGETID (FK) BUDGETCATEGORYID (FK) DATECREATED DATEMODIFIED
This table maintains a many-to-many relationship between Budget and BudgetCategory tables.
I don't want to create a class for the above association table and am trying to insert into this table. On top of it we generate our own primary keys. My question is if i set the generator class to be "assigned" for the IDBAG how can i assign the actual value to the column in the code, i am totally stumped here.
This is how the mapping looks like
<hibernate-mapping package="test.dao"> <class name="BudgetCategory" table="BUDGETCATEGORY"> <id name="budgetCategoryId" column="BUDGETCATEGORYID" type="string"> <generator class="assigned"/> </id> <property name="name" column="NAME" type="string"/> <property name="displayName" column="DISPLAYNAME" type="string"/> <property name="type" column="TYPE" type="string"/> <property name="dateCreated" column="DATECREATED" type="timestamp"/> <property name="dateModified" column="DATEMODIFIED" type="timestamp"/>
<set name="tips" inverse="true" cascade="all-delete-orphan"> <key column="BUDGETCATEGORYID"/> <one-to-many class="test.dao.BudgetTip"/> </set>
<idbag name="budgets" table="BUDGETTOBUDGETCATEGORY"> <collection-id column="BUDGETTOBUDGETCATEGORYID" type="string"> <generator class="assigned"/> </collection-id> <key column="BUDGETCATEGORYID"/> <many-to-many column="BUDGETID" class="test.dao.Budget"/> </idbag> </class> </hibernate-mapping>
Here's the code snippet that i am trying to execute and have been stumped on how to add our application's primary key to the collection-id column
//Code Snippet
public BudgetCategory save(Object persistentObject) throws DataException { if (persistentObject == null) { throw new DataException("Cannot save a null BudgetCategory object"); } BudgetCategory category = (BudgetCategory)persistentObject; category.setBudgetCategoryId(IdUtils.randomStr(ID_DEFAULT_LENGTH)); //while saving a new BudgetCategory instance make sure that the category is //added to every single Budget instance.
//TODO:: Add the surrgate key id to the collection-id column using IdUtils
category.setBudgets(getHibernateTemplate().loadAll(Budget.class)); return (BudgetCategory)saveOrUpdate(category); }
Any help here would be appreciated. We are using Hibernate 3.0 with Spring.
thanks
- k
|