Persistence::Manual::LOB - Large objects attributes persisitence.
This manual explains how to create map between LOBs database column and object attributes.
At the first stage lets define entity that represents logical unit of data in the database.
my $photo_entity = Persistence::Entity->new( name => 'photo', alias => 'ph', primary_key => ['id'], columns => [ sql_column(name => 'id'), sql_column(name => 'name', unique => 1), ], lobs => [ sql_lob(name => 'blob_content', size_column => 'doc_size'), ] );
LOBs column if defined by sql_lob method that takes name - column name that stores a lob size_column - column name that sotres the lob size.
At the second state we have to define mapping between object and sql entity. We are using lob method that teks LOB column name, attribute , fetch method as parameters.
package Photo; use Abstract::Meta::Class ':all'; use Persistence::ORM ':all'; entity 'photo'; column 'id' => has('$.id'); column 'name' => has('$.name'); lob 'blob_content' => (attribute => has('$.image'), fetch_method => LAZY);
If you do not want to interact directly with ORM or Entity meta protocal to declare map between your class and entity, or entity and database you can alternatively use an XML mapping file to declare this metadata.
Perl class
package Photo; use Abstract::Meta::Class ':all'; has '$.id'; has '$.name'; has '$.image';
XML injection of the persistence metadata.
use Persistence::Meta::XML; my $meta = Persistence::Meta::XML->new(persistence_dir => 'meta/'); $meta->inject('persistence.xml');
XML definitions:
persistence.xml <?xml version="1.0" encoding="UTF-8"?> <persistence name="test" connection_name="test" > <entities> <entity_file file="photo.xml" /> </entities> <mapping_rules> <orm_file file="Photo.xml" /> </mapping_rules> </persistence> photo.xml <?xml version="1.0" encoding="UTF-8"?> <entity name="photo" alias="p"> <primary_key>id</primary_key> <columns> <column name="id" /> <column name="name" unique="1" /> </columns> <lobs> <lob name="blob_content" size_column="doc_size" /> </lobs> </entity> Photo.xml <?xml version="1.0" encoding="UTF-8"?> <orm entity="photo" class="Photo" > <column name="id" attribute="id" /> <column name="name" attribute="name" /> <lob name="blob_content" attribute="image" fetch_method="LAZY" /> </orm>
To install Persistence::Entity, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Persistence::Entity
CPAN shell
perl -MCPAN -e shell install Persistence::Entity
For more information on module installation, please visit the detailed CPAN module installation guide.