Spring-data-jpa storing blob

后端 未结 6 907
梦如初夏
梦如初夏 2021-02-04 14:17

What is \"best\" or canonical way to store entity with blob using spring-data-jpa?

@Entity
public class Entity {
  @Id
  private Long id;
  @Lob()
  private Blob         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 15:08

    Spring Data does not handle BLOBs but Spring Content does. Specifically, Spring Content JPA stores content as BLOBs in the database and associates that content with an Entity through annotations.

    pom.xml

       
       
          com.github.paulcwarren
          spring-content-jpa-boot-starter
          0.0.11
       
       
       
          com.github.paulcwarren
          spring-content-rest-boot-starter
          0.0.11
       
    

    Entity.java

    @Entity
    public class Entity {
       @Id
       @GeneratedValue
       private long id;
    
       @ContentId
       private String contentId;
    
       @ContentLength
       private long contentLength = 0L;
    
       // if you have rest endpoints
       @MimeType
       private String mimeType = "text/plain";
    

    DataContentStore.java

    @StoreRestResource(path="data")
    public interface DataContentStore extends ContentStore {
    }
    

    The advantage of this approach over the accepted answer is that the Developer doesn't need to worry about any of the boilerplate code (the "service" in the accepted answer). The BLOB is also exposed as Spring Resource giving a natural programming interface. Or can be automatically exported via a REST interface. But none of this requires any coding, on behalf of the Developer, besides java config and the store interface.

提交回复
热议问题