Thursday, 4 May 2017

Domain Model in Hibernate

Domain Model in Hibernate

The application domain model is the central character in an ORM. Domain Model are the classes we would be mapping in our application. These domain model classes needs to follow the Plain Old Java Object (POJO) / JavaBean programming model in order to make Hibernate works best.
Applications using Hibernate uses its proprietary XML mapping file format for this purpose. With JPA, most of this information is now defined in a way that is portable across ORM/JPA providers using annotations and/or standardized XML format.

Mapping types
Hibernate understands both the Java and JDBC representations of application data. The ability to read/write this data from/to the database is the function of a Hibernate type. A type, is an implementation of the org.hibernate.type.Type interface.
To help understand the type categorizations, we take an example of a simple table and domain model that we wish to map.

create table ContactInfo (
    id integer not null,
    firstName varchar(255),
    lastName varchar(255),
    middleName varchar(255),
    notes varchar(255),
    starred boolean not null,
    website varchar(255),
    primary key (id)

@Entity(name = "ContactInfo")
public static class ContactInfo {

    private Integer id;
    private Name name;
    private String notes;
    private URL website;
    private boolean starred;

    //Getters and setters

public class Name {

    private String firstName;
    private String middleName;
    private String lastName;

    // getters and setters

In the broadest sense, Hibernate categorizes types into two groups:
  • Value types
  • Entity types

Value types
A value type is a piece of data that does not define its own lifecycle. It is, owned by an entity, which defines its lifecycle. All the state of an entity is made up entirely of value types. These state fields or JavaBean properties are termed persistent attributes. The persistent attributes of the ContactInfo class are value types.

Value types are further classified into three sub-categories:

Basic types
In mapping the ContactInfo table, all attributes except for name would be basic types.

Embeddable types
The name attribute is an example of an embeddable type.

Collection types
Collection types are also a distinct category among value types.

Entity types
Entities, exist independently of other objects whereas values do not. Entities are domain model classes which correlate to rows in a database table, using a unique identifier. Entities exist independently and define their own lifecycle. The ContactInfo class is an example of an entity. 

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...