Friday, October 5, 2012

Basic O/R Mapping

Mapping Declaration:
Object Relational mappings bisa kita definisikan dalam 3 pendekatan.
1. using Java 5 annotations (via the Java Persistence 2 annotations)
2. using JPA 2 XML deployment descriptors (described in chapter XXX)
3. using the Hibernate legacy XML files approach known as hbm.xml



Annotations dibedakan dalam 2 kategori, the logical mapping annotations (describing  objectmodel, the association between two entities etc.) and the physical mapping annotations (describing the physical schema, tables, columns, indexes, etc). Dibawah kita akan menggabungkan antar kedua category tadi.
JPA annotations nya ada di javax.persistence.* package. Trus Hibernate specific extensionsnya ada di org.hibernate.annotations.*. 

Nih contohnya mapping sederhananya:


package eg;
@Entity
@Table(name="cats") @Inheritance(strategy=SINGLE_TABLE)
@DiscriminatorValue("C") @DiscriminatorColumn(name="subclass", discriminatorType=CHAR)
public class Cat {
@Id @GeneratedValue
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
private Integer id;
public BigDecimal getWeight() { return weight; }
public void setWeight(BigDecimal weight) { this.weight = weight; }
private BigDecimal weight;
@Temporal(DATE) @NotNull @Column(updatable=false)
public Date getBirthdate() { return birthdate; }
public void setBirthdate(Date birthdate) { this.birthdate = birthdate; }
private Date birthdate;
@org.hibernate.annotations.Type(type="eg.types.ColorUserType")
@NotNull @Column(updatable=false)
public ColorType getColor() { return color; }
public void setColor(ColorType color) { this.color = color; }
private ColorType color; 
@NotNull @Column(updatable=false)
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
private String sex;
@NotNull @Column(updatable=false)
public Integer getLitterId() { return litterId; }
public void setLitterId(Integer litterId) { this.litterId = litterId; }
private Integer litterId;
@ManyToOne @JoinColumn(name="mother_id", updatable=false)
public Cat getMother() { return mother; }
public void setMother(Cat mother) { this.mother = mother; }
private Cat mother;
@OneToMany(mappedBy="mother") @OrderBy("litterId")
public Set<Cat> getKittens() { return kittens; }
public void setKittens(Set<Cat> kittens) { this.kittens = kittens; }
private Set<Cat> kittens = new HashSet<Cat>();
}
@Entity @DiscriminatorValue("D")
public class DomesticCat extends Cat {
public String getName() { return name; }
public void setName(String name) { this.name = name }
private String name;
}
@Entity
public class Dog { ... }
Pendekatan hbm.xml menggunakan skema XML yang dirancang untuk dapat dibaca dan diedit.
Bahasa pemetaan Java-centris, yang berarti bahwa that mappings are constructed around persistentclass declarations and not table declarations
Harap dicatat bahwa meskipun banyak pengguna Hibernate yang memilih untuk menulis XML dengan manual, ada beberapa tools untuk menghasilkan dokumen pemetaan. Misalnya XDoclet, Middlegen dan AndroMDA .

Contoh:


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="eg">
<class name="Cat"
table="cats"
discriminator-value="C">
<id name="id">
<generator class="native"/>
</id>
<discriminator column="subclass"
type="character"/>
<property name="weight"/>
<property name="birthdate"
type="date"
not-null="true"
update="false"/>
<property name="color"
type="eg.types.ColorUserType"
not-null="true"
update="false"/>
<property name="sex"
not-null="true"
update="false"/>
<property name="litterId"
column="litterId"
update="false"/>
<many-to-one name="mother"
column="mother_id"
update="false"/>
<set name="kittens"
inverse="true"
order-by="litter_id">
<key column="mother_id"/>
<one-to-many class="Cat"/>
</set>
<subclass name="DomesticCat"
discriminator-value="D">
<property name="name"
type="string"/>
</subclass>
</class>
<class name="Dog">
<!-- mapping for Dog could go here -->
</class>
</hibernate-mapping>
Sekarang gw mau bahas tentang kosep mapping itu sendiri (both annotations and XML). We
will only describe, however, the document elements and attributes that are used by Hibernate at runtime. The mapping document also contains some extra optional attributes and elements that affect the database schemas exported by the schema export tool (for example, the not-null attribute).

Entity
Entity adalah object java biasa yang biasa disebut dengan POJO, which will be persisted by Hibernate
Deklarasi entity sangat mudah sob, cuma pake @entity anotations.


@Entity
public class Flight implements Serializable {
Long id;
@Id
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
}
That's pretty much it, the rest is optional. There are however any options to tweak your entity mapping, let's explore them.
@Table lets you define the table the entity will be persisted into. If undefined, the table name is the unqualified class name of the entity. You can also optionally define the catalog, the schema as well as unique constraints on the table.


@Entity
@Table(name="TBL_FLIGHT",
schema="AIR_COMMAND",
uniqueConstraints=
@UniqueConstraint(
name="flight_number",
columnNames={"comp_prefix", "flight_number"} ) )
public class Flight implements Serializable {
@Column(name="comp_prefix")
public String getCompagnyPrefix() { return companyPrefix; }
@Column(name="flight_number")
public String getNumber() { return number; }
}

The constraint name sifatnya optional (digenerate if left undefined). The column names composing the constraint correspond to the column names as defined before the Hibernate NamingStrategy is applied.
@Entity.name lets you define the shortcut name of the entity you can used in JP-QL and HQL queries. It defaults to the unqualified class name of the class.

Pokoknya gitulah kira-kira basic mapping pake Hibernate yang sekarang gw lg belajarin, itu baru dua point doang (cara deklarasi mapping dan entitas) hohoho masih banyak bgt ternyata. Kalo mau lengkapnya silakan klik disini.

0 komentar:

Post a Comment

 
Design by Frits Hendrico Tarihoran | Bloggerized by fritshendrico - Premium Blogger Themes | Revivalist, History Maker