JPA, Mysql Blob returns data too long

后端 未结 3 1163
情歌与酒
情歌与酒 2020-12-07 20:19

I\'ve got some byte[] fields in my entities, e.g.:

@Entity
public class ServicePicture implements Serializable {
    private static final long s         


        
相关标签:
3条回答
  • 2020-12-07 20:31

    In our case we had to use the following syntax:

    public class CcpArchive
    {
        ...
        private byte[] ccpImage;
        ...
        @Lob
        @Column(nullable = false, name = "CCP_IMAGE", columnDefinition="BINARY(500000)")
        public byte[] getCcpImage()
        {
            return ccpImage;
        }
        ...
    }
    
    0 讨论(0)
  • 2020-12-07 20:34

    I use below and it works for images

    @Lob
    @Column(name = "file", columnDefinition = "LONGBLOB")
    private byte[] file;
    
    0 讨论(0)
  • 2020-12-07 20:50

    It all depends on the column type used for the picture column. Depending on your needs, use a:

    • TINYBLOB: maximum length of 255 bytes
    • BLOB: maximum length of 65,535 bytes
    • MEDIUMBLOB: maximum length of 16,777,215 bytes
    • LONGBLOB: maximum length of 4,294,967,295 bytes

    Note that if you generate your table from the JPA annotations, you can "control" the type MySQL will use by specifying the length attribute of the Column, for example:

    @Lob @Basic(fetch = FetchType.LAZY)
    @Column(length=100000)
    private byte[] picture;
    

    Depending on the length, you'll get:

           0 < length <=      255  -->  `TINYBLOB`
         255 < length <=    65535  -->  `BLOB`
       65535 < length <= 16777215  -->  `MEDIUMBLOB`
    16777215 < length <=    2³¹-1  -->  `LONGBLOB`
    
    0 讨论(0)
提交回复
热议问题