I try to simplify the selection of a collection with this pseudo-code.
I think can be of some help for a beginner like me, but may be I have some errors. Can you verify this?
Thanks.
Code:
// --------------------------------------------------------------------------
// Pseudo-code collection selector for hibernate
// author: Antonio Vazquez Araujo
// e-mail: formatic@mundo-r.com
// --------------------------------------------------------------------------
// NOTES:
// cascade meening:
// save-update : save|update parent -> save|update childs
// delete : delete parent -> delete childs
// all : save|update|delete parent -> save|update|delete childs
// all-delete-orphan: unlink one-to-many child -> delete child
// --------------------------------------------------------------------------
// Select the relation type
if(relation == many-to-one ){
<many-to-one
name = "propertyName"
column = "parent_id"
class = "ParentClass"
cascade = "save-update"|"delete"|"all"|"all-delete-orphan"
/>
// Bidirectionality
if(relation must be bidirectional){
// In the ONE side of the relation:
<collectionType
name="propertyName"
inverse = "true"
/>
<key
// THE SAME COLUMN
column="parent_id"
/>
<one-to-many
class="ChildClass"
/>
// The operations in the inverse side are NOT persisted.
}
}else if (relation == collection-of-values|one-to-many|many-to-many){
// Select the collection type and assign it a name
<collectionType // bag|set|list|map|array
name="name"
cascade=
"save-update"|"delete"|"all"|"all-delete-orphan"
// Select a name for the key column in the children's table
<key
column="parent_id"
/>
// Select the relation type
if(type of relation == one to many){
// DONT NEED A NEW TABLE
// Simply select the class of children objects
<one-to-many
class = "ChildrenClass"
/>
}else if( type of relation == collection){
// A NEW TABLE WILL BE CREATED FOR THE RELATION
if(is an indexed collection){ // (list|map|array)
if(index type == value){
<index
column="index_column"
type = "index_type"
/>
}else if(index type == entity){
<index-many-to-many
column="index_column"
class = "index_Class"
/>
}
}else{
// Not indexed collection (set|bag)
}
if(type of elements == value){
<element
column="element_column"
type = "element_type"
/>
}else if(type of elements == entity){
<many-to-many
column="element_column"
class = "element_Class"
/>
}
}
</collectionType>
if(relation must be bidirectional){
if(relation == one-to-many){
// In the other side of the relation:
// Declare another many-to-one relation
// 2- whith inverse = "true"
// 3- with the many-to-many column == this relation key column
// The operations in the inverse side are NOT persisted.
}else if(relation == many-to-many){
// In the other side of the relation:
// Declare another many-to-many relation
// 1- pointing to the same table
// 2- whith inverse = "true"
// 3- with the many-to-many column == this relation key column
// The operations in the inverse side are NOT persisted.
}else{
// values collection. Can not be bidirectional
}
}
}