Annotate non persistent properties with the @Transient Annotation

In Most of the cases, all the Java bean properties (example, firstname, lastname, email, etc.) that need to be stored to the database. But in some situation, we might have other properties that do not need to be stored in the database. In this case, we need to say to hibernate that do not take this property for all the Database operation. This can be done using @Transient annotation.

By default, all fields in your class will be persisted to the database if possible. If there is a field that should not be recorded in the database, mark that field with the @Transient annotation

Now let us do an example on the above subject

Step 1:

Create the hibernate setup as shown here;

Step 2:

Now let us modify our by adding a non persistent field

package mypack;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Transient;

@Table(name = "patient")
public class patient {

    private Integer id;
    private String firstName;
    private String lastName;
    private String password;

    public String getPassword() {
        return password;

    public void setPassword(String password) {
        this.password = password;

    @Column(name = "ID")
    public Integer getId() {
        return id;

    public void setId(Integer id) { = id;

    @Column(name = "firstname")
    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    @Column(name = "lastname")
    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

Step 3:

Let us modify to create new patient as follows

package mypack;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.HibernateException;

import Utility.HibernateUtil;

public class Test {

private static SessionFactory factory;
private static ServiceRegistry serviceRegistry;

public static void main(String[] args) {

public static void getAllRecords() {
List allpatients;
Session session = HibernateUtil.beginTransaction();
Query q1 = session.createQuery("from patient");
allpatients = q1.list();
for (int i = 0; i < allpatients.size(); i++) {
patient pt = (patient) allpatients.get(i);

public static void createpatient() {

Session session = HibernateUtil.beginTransaction();
patient p1 = new patient();


Step 4:

Run the as Java Application and the following is the output

Hibernate: insert into patient (firstname, lastname) values (?, ?)
Hibernate: select patient0_.ID as ID0_, patient0_.firstname as firstname0_, patient0_.lastname as lastname0_ from patient patient0_

Another good example

public enum Gender { MALE, FEMALE, UNKNOWN }

public Person {
private Gender g;
private long id;

public long getId() { return id; }
public void setId(long id) { = id; }

public Gender getGender() { return g; }
public void setGender(Gender g) { this.g = g; }

public boolean isMale() {
return Gender.MALE.equals(g);

public boolean isFemale() {
return Gender.FEMALE.equals(g);