@Lob Annotation in Hibernate Example

@Lob Annotation in Hibernate

In this tutorial, you will learn about simple implementation of @Lob annotation in hibernate example. This particular example were tested with MySQL database server environment and all programs and output has been shared in the same post.

The @Lob stands for Large Object. Sometimes we may have to store the large amount of character to specific column in the table (eg: Project Description, Product Description, Book Information and etc.). When we have the string field in a entity class, The hibernate framework will create the table with string fields as varchar datatype with maximum value of 255 (ie: bookDescription varchar(255)) by default. But they are may not be useful when we wants to store large information(more characters) to the specific column in a table.

If we add the @LOB annotation to string field it will be changed as longtext from the varchar datatype in table, then we can able to store the large amount of character to specific column. The following steps contains simple implementation of @Lob annotation in hibernate.

Project Structure

@LOB Annotation in Hibernate

1) Getting the session from session factory

package com.dineshkrish.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

 * @author Dinesh Krishnan

public class SessionProvider {

	private static Session session;
	private static Configuration configuration;
	private SessionProvider() { }
	static {
		configuration = new Configuration()
		.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
	    .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
	    .setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/dineshkrish")
	    .setProperty("hibernate.connection.username", "root")
	    .setProperty("hibernate.connection.password", "")
	    .setProperty("hibernate.show_sql", "true")
	    .setProperty("hibernate.hbm2ddl.auto", "update")
	    // Your Mapping Class
	public static Session getSession() {
		SessionFactory factory = configuration.buildSessionFactory();
		session = factory.openSession();
		return session;

2) Entity class with @Lob annotation

package com.dineshkrish.hibernate;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;

public class Book {

	private int bookId;
	private String bookName;
	private String bookDescription;

	public int getBookId() {
		return bookId;

	public void setBookId(int bookId) {
		this.bookId = bookId;

	public String getBookName() {
		return bookName;

	public void setBookName(String bookName) {
		this.bookName = bookName;

	public String getBookDescription() {
		return bookDescription;

	public void setBookDescription(String bookDescription) {
		this.bookDescription = bookDescription;


3) Persisting the book entity to the database table

package com.dineshkrish.hibernate;

import org.hibernate.Session;

public class BookApplication {

	public static void main(String[] args) {

		// Defining the Object
		Book book = new Book();

		// Setting the values for attributes
		book.setBookName("Java Example Book");
		// setting the large amount of characters for bookDescription field
		// it contains more than 1000+ characters
		book.setBookDescription("Lorem Ipsum is simply dummy text of the printing and "
				+ "typesetting industry. Lorem Ipsum has been the industry's "
				+ "standard dummy text ever since the 1500s, when an unknown printer "
				+ "took a galley of type and scrambled it to make a type specimen book. "
				+ "It has survived not only five centuries, but also the leap into electronic "
				+ "typesetting, remaining essentially unchanged. It was popularised in the 1960s"
				+ " with the release of Letraset sheets containing Lorem Ipsum passages, and more "
				+ "recently with desktop publishing software like Aldus PageMaker "
				+ "including versions of Lorem Ipsum. It is a long established fact "
				+ "that a reader will be distracted by the readable content "
				+ "of a page when looking at its layout. The point of using Lorem Ipsum "
				+ "is that it has a more-or-less normal distribution of letters,"
				+ " as opposed to using 'Content here, content here', making it look "
				+ "like readable English. Many desktop publishing packages and web page "
				+ "editors now use Lorem Ipsum as their default model text, and a search "
				+ "for 'lorem ipsum' will uncover many web sites still in their infancy. "
				+ "Various versions have evolved over the years, sometimes by accident, "
				+ "sometimes on purpose (injected humour and the like).");

		Session session = SessionProvider.getSession();


		// saving the object




Download Source Code

you can download the entire source code here


@LOB Annotation in Hibernate

@LOB Annotation in Hibernate


1. JPA API Documentation
2. @Lob Annotation Documentation