HIBERNATE TUTORIAL

HIBERNATE - Getting Started With Hibernate

Creating persistent java objects

Hibernate works best with the Plain Old Java Objects programming model for persistent classes.

Hibernate is not restricted in its usage of property types, all Java JDK types and primitives (like String, char and Date) can be mapped, including classes from the Java collections framework. You can map them as values, collections of values, or associations to other entities. The id is a special property that represents the database identifer (primary key) of that class, Hibernate can use identifiers only internally, but we would lose some of the flexibility in our application architecture.

No special interface has to be implemented for persistent classes nor do you have to subclass from a special root persistent class. Hibernate also doesn't require any build time processing, such as byte-code manipulation, it relies solely on Java reflection and runtime class enhancement (through CGLIB). So, without any dependency of the POJO class on Hibernate, we can map it to a database table.

Following code sample represents a java object structure which represents the AppLabsUser table. Generally these domain objects contain only getters and setters methods. One can use Hibernate extension toolset to create such domain objects.

AppLabsUser.java

package org.applabs.quickstart;

import java.io.Serializable;
import java.util.Date;
import org.apache.commons.lang.builder.ToStringBuilder;

public class AppLabsUser implements Serializable {
   

public void setName(String name) {
/** identifier field */
private Long id;

/** persistent field */
private String userName;

/** persistent field */
private String userPassword;

/** persistent field */
private String userFirstName;

/** persistent field */
private String userLastName;

/** persistent field */
private String userEmail;

/** persistent field */
private Date userCreationDate;

/** persistent field */
private Date userModificationDate;

/** full constructor */
public Applabsuser(String userName, String userPassword, String userFirstName, String userLastName, String userEmail, Date userCreationDate, Date userModificationDate) {
this.userName = userName;
this.userPassword = userPassword;
this.userFirstName = userFirstName;
this.userLastName = userLastName;
this.userEmail = userEmail;
this.userCreationDate = userCreationDate;
this.userModificationDate = userModificationDate;
}

/** default constructor */
public Applabsuser() {
}

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public String getUserName() {
return this.userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserPassword() {
return this.userPassword;
}

public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}

public String getUserFirstName() {
return this.userFirstName;
}

public void setUserFirstName(String userFirstName) {
this.userFirstName = userFirstName;
}

public String getUserLastName() {
return this.userLastName;
}

public void setUserLastName(String userLastName) {
this.userLastName = userLastName;
}

public String getUserEmail() {
return this.userEmail;
}

public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}

public Date getUserCreationDate() {
return this.userCreationDate;
}

public void setUserCreationDate(Date userCreationDate) {
this.userCreationDate = userCreationDate;
}

public Date getUserModificationDate() {
return this.userModificationDate;
}

public void setUserModificationDate(Date userModificationDate) {
this.userModificationDate = userModificationDate;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}

}// End of class



 

Hibernate tutorial
Introduction to Hibernate
Overview of Hibernate
Features of Hibernate
Getting Started with Hibernate
Prepare Database
Create Persistent Java Objects
Mapping POJO With Persistence Layer Using Hibernate Mapping Document
Hibernate Configuration File
Hibernate Sample Code (Inserting new record)
Hibernate Sample Code (Quering the database)
Hibernate O/R Mapping
Hibernate Mapping In Depth
Hibernate Query language
Hibernate Complete Example

Use of this website constitutes acceptance of the AllAppLabs.com Terms and Conditions