Persistence-Entity
view release on metacpan or search on metacpan
lib/Persistence/Manual/Relationship.pm view on Meta::CPAN
],
);
#relationsips definition here
$project_entity->add_to_many_relationships(
sql_relationship(target_entity => $emp_project_entity,join_columns => ['projno'], order_by => 'projno, empno')
);
$emp_entity->add_to_many_relationships(
sql_relationship(target_entity => $emp_project_entity, join_columns => ['empno'], order_by => 'empno, projno')
);
$dept_entity->add_to_many_relationships(
sql_relationship(target_entity => $emp_entity, join_columns => ['deptno'], order_by => 'deptno, empno')
);
$dept_entity->add_to_one_relationships(
sql_relationship(target_entity => $address_entity, join_columns => ['addr_id'])
);
$entity_manager->add_entities($emp_project_entity, $emp_entity, $project_entity, $dept_entity, $address_entity);
Note:
When adding to_many relationsnship,on the other side reflective to_one relationship. is created automaticaly.
=head2 Objects Relationships
From database entities point of view we may define the following relationships.
=over
=item one_to_one
An example of a one-to-one relationship is one between a Department object
and an Address object. In this example, each Department has exactly one Address,
and each Address has exactly one Department.
package Address;
use Abstract::Meta::Class ':all';
use Persistence::ORM ':all';
entity 'address';
column id => has('$.id');
column loc => has('$.location');
column town => has('$.town');
column postcode => has('$.postcode');
package Department;
use Abstract::Meta::Class ':all';
use Persistence::ORM ':all';
entity 'dept';
column deptno => has('$.id');
column dname => has('$.name');
to_one 'address' => (
attribute => has ('$.address', associated_class => 'Address'),
cascade => ALL,
fetch_method => EAGER,
);
=item many_to_one
An example of a many-to-one relationship is one between an Employee object
and a Department object. In this example, each Employee has exactly one Department,
and each Department has many Employees.
package Employee;
use Abstract::Meta::Class ':all';
use Persistence::ORM ':all';
entity 'emp';
column empno=> has('$.id');
column ename => has('$.name');
column job => has '$.job';
to_one 'dept' => (
attribute => has ('$.dept', associated_class => 'Department'),
cascade => ALL,
fetch_method => EAGER,
);
package Department;
use Abstract::Meta::Class ':all';
use Persistence::ORM ':all';
entity 'dept';
column deptno => has('$.id');
column dname => has('$.name');
Note: Bidirectional relationship in this case requires reference on Dept object:
one_to_many 'emp' => (
attribute => has('@.employees' => (associated_class => 'Employee')),
fetch_method => EAGER,
cascade => ALL,
);
=item one_to_many
An example of a one-to-many relationship is one between a Department object
and Employees objects. In this example, each Department has many Employees
and each Employee has exactly one Department
package Department;
use Abstract::Meta::Class ':all';
use Persistence::ORM ':all';
entity 'dept';
column deptno => has('$.id');
column dname => has('$.name');
one_to_many 'emp' => (
attribute => has('@.employees' => (associated_class => 'Employee')),
fetch_method => EAGER,
cascade => ALL,
);
package Employee;
use Abstract::Meta::Class ':all';
use Persistence::ORM ':all';
entity 'emp';
column empno=> has('$.id');
column ename => has('$.name');
column job => has '$.job';
Note: Bidirectional relationship in this case requires reference on Employee object:
to_one 'dept' => (
attribute => has ('$.dept', associated_class => 'Department'),
cascade => ALL,
fetch_method => EAGER,
);
=item many_to_many
An example of a mant to mant relationship is one between an Employee and a Project.
A Employee can be associated to many projects and each Projects has many Employees.
Many to many relationship uses join table.
package Employee;
use Abstract::Meta::Class ':all';
use Persistence::ORM ':all';
entity 'emp';
column empno=> has('$.id');
column ename => has('$.name');
column job => has '$.job';
many_to_many 'project' => (
attribute => has('%.projects' => (associated_class => 'Project'), index_by => 'name'),
join_entity_name => 'emp_project',
fetch_method => LAZY,
cascade => ALL,
);
package Project;
use Abstract::Meta::Class ':all';
use Persistence::ORM ':all';
entity 'project';
column projno => has('$.id');
column name => has('$.name');
=back
=cut
( run in 0.504 second using v1.01-cache-2.11-cpan-2398b32b56e )