Home > OS >  What difference does it make to put an annotation @Lob or not?
What difference does it make to put an annotation @Lob or not?

Time:05-16

There is a request table in my database. Here is its structure

enter image description here

Usually I created the entity with my hands, but this time I decided to generate it from the database. Here's what I got

@Entity
@Table(name = "request")
public class Request
{
    @Id
    @Column(name = "id", nullable = false)
    private Integer id;

    @Lob
    @Column(name = "body")
    private String body;

    @Column(name = "description", length = 10000)
    private String description;

    @Lob
    @Column(name = "headers")
    private String headers;

    @Column(name = "http_method", nullable = false, length = 20)
    private String httpMethod;

    @Column(name = "name", nullable = false, length = 100)
    private String name;

    @Lob
    @Column(name = "path_variables")
    private String pathVariables;

    @Column(name = "port")
    private Integer port;

    @Lob
    @Column(name = "query_params")
    private String queryParams;

    @Lob
    @Column(name = "url", nullable = false)
    private String url;

I noticed that a strange annotation @Lob appeared. I've never seen her before. I decided to Google what it means and why to put it. I found a lot of answers and they all said that this annotation is needed so that Hibernate understands that there may be a big object here.

But it seems to me that this annotation does nothing. After all, regardless of whether it is worth it or not, everything works the same?

What is it for?

CodePudding user response:

From the looks of it, your entity generation tool just marked with @Lob all the columns that don't have the length specified - most likely just to make sure that whatever data you put into them it will fit.
(Say, in Oracle maximum BLOB/CLOB size is (4 GB - 1) * DB_BLOCK_SIZE initialization parameter (8 TB to 128 TB))

As for the @Lob itself, here's what javadoc says:

Specifies that a persistent property or field should be persisted as a large object to a database-supported large object type. Portable applications should use the Lob annotation when mapping to a database Lob type. The Lob annotation may be used in conjunction with the Basic annotation or the ElementCollection annotation when the element collection value is of basic type. A Lob may be either a binary or character type.

The Lob type is inferred from the type of the persistent field or property, and except for string and character-based types defaults to Blob.

The common usecase for binary lobs (BLOBs) is storing some binary file content in the database, say an image, and for the character lobs (CLOBs) - large texts documents.

  • Related