-
Notifications
You must be signed in to change notification settings - Fork 0
GridFS support with Kundera MongoDB
[GridFS] (http://docs.mongodb.org/manual/core/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.
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
Refer this [test case] (https://github.com/impetus-opensource/Kundera/blob/trunk/src/kundera-mongo/src/test/java/com/impetus/client/crud/gfs/GridFSTest.java).
- CRUD on simple entities (without relationships, inheritance, embeddable) is possible.
- Only select Queries are allowed with
WHERE
andORDER BY
clauses. - Lob field can only be
byte[]
.
- Enable support for
java.io.File
type field as Large Object. - Enable support for multiple large objects in a single entity.