Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavaHibernate – @Lob Annotation

Hibernate – @Lob Annotation

Many times there is a scenario while storing the data in the database that we have to store images or files within our database for a specific entity. In that case, we can use @Lob annotation which will help us to map large binary objects or large character objects to a specific entity in our database. We can store images or files in our database by converting them into large binary objects. The @Lob annotation indicates that the property should be stored in the database in the form of a large object type in the database.

Examples for @Lob Annotation

Example 1:

Java




// on the below line creating an entity for employee
@Entity
public class Employee {
    // on the below line creating variables for employee id,
    // name, age and photo of the employee.
    @Id
      @GeneratedValue
      private int empID;
    private String name;
    private int age;
      
      @Lob
      private byte[] photo;
  
    // on the below line creating a constructor.
    public Employee(String name, int age, byte[] photo)
    {
        this.name = name;
        this.age = age;
        this.photo = photo;
    }
}


Code Explanation:

In the above example, we are creating an entity class for Employees in which we are creating different fields. The first field is for employee id for which we are adding an annotation of @Id and @GeneratedValue to mark it as id and generate the value for it automatically. After that we are creating a field as the name for getting employee name then create a field for age to get employee age. Lastly, creating a field name as photo which has a data type of byte which is used to get the photo for the employee in the form of byte. We are annotating this photo field with @Lob to indicate that this field might be stored in the form of a blob in our database.

Example 2:

Java




// on the below line creating an entity for patient
@Entity
public class Patient {
    // on the below line creating variable for id, name,
    // blood group and report for patients reports.
    @Id 
      @GeneratedValue
      private int id;
    private String name;
    private String bloodGroup;
      
      @Lob
      private byte[] reports;
  
    // on the below line creating a constructor.
    public Patient(String name, String bloodGroup,
                   byte[] reports)
    {
        this.name = name;
        this.bloodGroup = bloodGroup;
        this.reports = reports;
    }
}


Code Explanation:

In the above example we are creating an entity class for Patients in which we are creating different fields. We are creating a field for id which tells us about the id for the patient. We are annotating id with @GeneratedValue and @Id to indicate it as the ID. After that we are creating a field for the name which indicates the name for the patient. Then we are creating a field for the blood group which is used to get the blood group for patients. Lastly we are creating a field for reports which is used to store the reports for the patients. We are marking this report’s annotation with @Lob to indicate that we have to store the reports as a blob in our database.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments