Posted by tcle on November 2, 2008
Overview
Active Record – adopted by Rails framework is reckoned to be a tough player in single table/object mind set. In short, it is a way to map database rows to objects so every columns then become properties of the objects. Static methods are available for finding instances. There are also methods that provides CRUD functions for you. You might find it nearly like 1:1 correspondent mapping between tables and classes, columns and fields.
Hibernate utilize the Data Mapper pattern which use a mapping layer to moves data between objects and database independently. It requires you to have hands-on job in configuring how these moves is done vie a XML file. The file is responsible for telling how objects and rows, columns and fields are mapped.
Active Record support native SQL while Hibernate make up its own object oriented Hibernate Query Language – HQL which will later be translate into SQL with optimization for the specific database. The intention for a new query language is a serious move and its worth in case of complex queries. Either one will not a big different to developers. Just dig into each and you find yourself comfortable.
IMHO
Hibernate and Active Record do the mapping in its own way. By some magical reflection, Active Record implicitly do the job for you. Just to make sure using it for simple domain models. Hibernate in the other hand explicitly require your efforts in mimicking the objects and database similarly in terms of columns to fields and associations.
Some people might argue on the native of Active Record as a data centric way because Active Record classes expose the data every from the database (e.g. every object fields is related to columns from database rows). I think it is not a big concern to bring up front because AR is doing good on what its meant to do. Hibernate in conclusion will be good choice for relational database or an existing legacy database schema case.
Posted in Design & Pattern, Rails, Ruby | Tagged: Active Record, Hibernate | Leave a Comment »
Posted by tcle on May 20, 2008
Aggregation
Aggregation is a kind of association that specifies a whole/part relationship between the aggregate (whole) and component part. This relationship between the aggregate and component is a weak “has a” relationship as the component may survive the aggregate object. The component object may be accessed through other objects without going through the aggregate object. The aggregate object does not take part in the lifecycle of the component object, meaning the component object may outlive the aggregate object. The state of the component object still forms part of the aggregate object.
An example of aggregation is a History-Class object contains zero or more of Student objects. The state of each Student object has an influence on the state of the History-Class object. If the History-Class object is destroyed, the Student objects may continue to exist.
The UML diagram above illustrates the aggregation association line with an open diamond on the aggregate side.
Composition
Composition is a kind of association very similar to aggregation except where the composite object has sole responsibility for the disposition of the component parts. The relationship between the composite and the component is a strong “has a” relationship, as the composite object takes ownership of the component. This means the composite is responsible for the creation and destruction of the component parts. An object may only be part of one composite. If the composite object is destroyed, all the component parts must be destroyed, or the reference and responsibility of the component part must be handed over to another object. Composition enforces encapsulation as the component parts usually are members of the composite object.
An example of composition is a House object contains zero or more Room objects. The state of each Room object has an influence on the House object. If the House object is destroyed, the Room objects will also be destroyed.
The UML diagram above illustrates the composition association line with a solid diamond on the composite side.
Source: http://www.javadesign.info/DesignConcepts/OOConcepts/aggregationcomposition
Posted in Design & Pattern | Tagged: UML | 3 Comments »