Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

Hibernate / Foreign Key Example?

Avatar

Former Community Member
First, read the disclaimer: I'm new at Hibernate so there's
likely an oversight on my part.



Can anyone offer an example or some pointers on this
scenario: Let's take the CRM example included with FDS. There is a
company table and employee table. Let's extend the CRM sample and
look at the company table. Perhaps I want to have a company table
and then add an industry table with a foreign key (instead of the
static array that is populated in the original example) to the
company table. Simple one (industry)-to-many (companies)
relationship, right?



So - my questions:



1) What's the suggested way to populate the industry combo
box with industry names? Do you just add the query during your app
initialization process?



2) (Bigger question for me) - how do you resolve the foreign
key in Flex? Do you have to build write the code in AS to resolve
the foreign key for industry and then populate the company table
with the proper industry id? I know you set up your company POJO
with the Industry object and I know Hibernate will handle some of
this, but I'm unclear on an elegant way to do this through Flex.



I'd REALLY, REALLY appreciate any discussion....thanks,

Headjoog







Now I've been able to set up Hibernate to query / populate
single tables per the example. BUT....if I wanted to populate the
combo box
3 Replies

Avatar

Level 3
Hi,



You should have another industries destination and your
company should have a industry property. In your destination, you
should have its association defined

<metadata>

<identity property="id"/>

<!-- it is many companies map to same industry -->

<many-to-one destination="allIndustries"
property="industry"/>

</metadata>



your Company.as should have

var public industry:Industry;



company.hbm.xml should have

<many-to-one name="industry" access="field"
class="your.java.IndustryClass" column="industryId"
cascade="save-update"/>



Company table

id, companyName, industryId .....







William Chan















Avatar

Level 3
Hi



For the comboBox, you can bind it to a managed
collection(filled by dataservice)



<mx:ComboBox id="cb" dataProvider="{allIndustries}"
labelField="name" change="company.industry = cb.selectedItem as
Industry"

selectedItem="{comboBoxSelect(allIndustries, 'name',
company)}" prompt="Select an Industry"/>



private static function comboBoxSelect(ac:ICollectionView,
field:String, value:*):Object

{

for (var i:Number = 0; i < ac.length; i++)

{

if(ac
[field] == value.industry.name)

return ac
;

}

return null;

}

Avatar

Former Community Member
Thanks so much - I was able to get this working. If I want to
show industry name in a data grid, along with the company name. do
you refer to it in the same manner as the combo box? When I try to
display in the data grid I just get the [Object object] displayed.



Again - thanks!