Friday, March 16, 2007

5 Sbb kenapa Spring

http://www.onjava.com/pub/a/onjava/2005/05/11/spring.html

5 Sbb kenapa Spring

10 Tips utk Belajar Hibernate (Kelas Permulaan)

  1. Create a new class, implementing the Serializable interface.
  2. Add class variables id and version, which are both of type Long. These variables are the primary key and the version number for optimistic locking respectively.
  3. Add any properties you need.
  4. Write a default constructor. All variables of type Collection, List, Set, ... should be initialized here.
  5. Generate getters and setters for all properties:
    1. id should have a public getter, and no setter
    2. version should have a protected getter, no setter
    3. variables of type Collection, List, Set, ... have a public getter and no setter. The public getter preferably return an unmodifiable list.
  6. Add annotations to the class variables rather than to the getters or setters.
    1. class-level: @Entity and maybe @Table(name=xxx). When using inheritance you will also need to specify the inheritance type.
    2. id: @Id and @GeneratedValue(strategy = GenerationType.AUTO)
    3. version: @Version
    4. variables: Hibernate Validator annotations such as @NotNull, @Length, @Range, ...
    5. variables: @Column(name=xxx), but only when the column name is not the same as the variable name
    6. variables: @OneToOne, @OneToMany, @ManyToOne or @ManyToMany in the case of relations.
  7. When using relations, make sure that the relation is bidirectional. For the sake of simplicity, I create utility methods that handle this on 1 side of the relation: properties of type Collection have utility methodes addXXX and removeXXX. The corresponding setter on the other side of the relation is protected, and called from the utility methods.
  8. Implement equals and hashcode methods.
    1. Always compare values using the getters of the properties, not the class variables directly.
    2. Do not compare id and version
    3. Do not compare Collections
  9. Add a mapping element to the hibernate.cfg.xml file
  10. You're done!