Dec 022013
 
create view of_your_tables_so_it_is 
    of an_object
    with object oid (your_relational_data) as
  select your_relational_data, 
         into_an_object_model
  from   your_relational_tables;
I'm not sure whether or not this approach is "better" than using Hibernate or other object-relational mapping tools.
At least you have a better idea how the mapping works by looking at the database I guess.

For further reading on this, have a look at this devshed article, here on oracle-base or Oracle’s object-relational dev guide.

For a script for the above, keep reading.

create table your_relational_tables (
  your_relational_data number, 
  into_an_object_model number
);

create or replace type an_object as object (
  your_relational_data number, 
  into_an_object_model number,
  member function multiply return number);
/
show errors

create or replace type body an_object as  
  member function multiply return number as
  begin
    return your_relational_data * 
             into_an_object_model;
  end; 
end;
/
show errors

insert into your_relational_tables
values (1, 1); 
insert into your_relational_tables
values (2, 2); 

-- create the view making the object
create view of_your_tables_so_it_is 
    of an_object
    with object oid (your_relational_data) as
  select your_relational_data, 
         into_an_object_model
  from   your_relational_tables;

select * from of_your_tables_so_it_is;

-- you can insert into the view "normally"...
insert into of_your_tables_so_it_is 
values (3, 3); 

-- ... or putting an object in
insert into of_your_tables_so_it_is 
values (an_object(4, 4)); 

select * from of_your_tables_so_it_is;

-- you can also get the object 
-- and access its functions
declare
  l an_object; 
begin
  select value(v)
  into   l
  from   of_your_tables_so_it_is v
  where  your_relational_data = 3;
  
  dbms_output.put_line(l.multiply);
end;
/

drop view of_your_tables_so_it_is;
drop type an_object;
drop table your_relational_tables purge;

  2 Responses to “Building Your Own ORM”

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)