Skip to content

GridFS support with Kundera MongoDB

Devender Yadav edited this page Dec 19, 2018 · 2 revisions

GridFS

GridFS is a MongoDB specification for storing and retrieving files that exceed the BSON-document size limit of 16MB.

Instead of storing a file in a single document, GridFS divides a file into chunks and stores each of those chunks as a separate document. By default, GridFS limits chunk size to 255k. GridFS uses two collections to store files. One collection stores the file chunks, and the other stores file metadata.

GridFS with Kundera

Kundera allows to perform CRUD and Query operation over MongoDB using GridFS. All you need to add @javax.persistence.Lob annotation on the field you want to insert using GridFS.

Entity

 @Entity
 public class Book
 {
    @Id
    private String id;

    @Column
    private String title;

    @Lob
   private byte[] pdfFile;

  //setters and getters
}

The benefit of using GridFS is that now you can store a file of size greater than 16MB.

Mapping

@Lob Field ==> GridFSInputFile

Other Fields ==> GridFSInputFile.metadata

Consider above mentioned Book Entity.

GridFSInputFile is created using pdfFile(@Lob field)

Other fields (id, title) are saved in metadata of GridFSInputFile

CRUD and Querying

Refer this test case.

Limitation

  • CRUD on simple entities (without relationships, inheritance, embeddable) is possible.
  • Only select Queries are allowed with WHERE and ORDER BY clauses.
  • Lob field can only be byte[].

To-Do

  • Enable support for java.io.File type field as Large Object.
  • Enable support for multiple large objects in a single entity.
Clone this wiki locally