Reputation: 2363
Say I have a Page entity which can have an array of associated Document entities: a straightforward one-to-many relationship.
<cfcomponent entityName="Page" persistent="true" table="pages">
<!--- A Page can have many Documents --->
<cfproperty name="document" fieldType="one-to-many" cfc="Document" fkColumn="pageID" inverse="true">
</cfcomponent>
However, each Document needs to know the path of its file system directory and the value of this property can vary according to context, therefore it is not persistent and needs to be passed in on instantiation.
<cfcomponent entityName="Document" persistent="true" table="documents">
<!--- This value needs to be set so the document knows its location --->
<cfproperty name="directoryPath" persistent="false">
<!--- Many Documents can belong to one Page --->
<cfproperty name="page" fieldType="many-to-one" cfc="Page" fkColumn="pageID">
<cffunction name="init" output="false">
<cfreturn this/>
</cffunction>
</cfcomponent>
If I were loading the array of documents for the page manually or using a Bean Factory, I could specify the directoryPath variable as an argument passed to the Document init() method. But here, the loading of the documents is done automatically by Hibernate.
Is there a way of passing init arguments to related objects when they are loaded by the ORM?
I know I can loop over the documents once loaded and specify the directory, and perhaps that's best practice, but passing the value to each on init seems more efficient. Is it possible?
Upvotes: 4
Views: 291
Reputation: 173
Looking through documentation it doesn't seem that there is a way to do what you ask.
One thing I would suggest is instead of looping through documents to set the property, you could set a property in Page object and access it from Document.
So after loading Page you would have something like Page.setDocumentPath(documentPath);
.
then when displaying documents you could have something like document.getPage().getDocumentPath();
.
Upvotes: 5