【问题标题】:Loading Image into byte array in entity bean将图像加载到实体 bean 中的字节数组中
【发布时间】:2015-08-19 04:23:22
【问题描述】:

我正在使用 spring data jpa 和 hibernate 从数据库表中检索实体。实体字段之一是位于文件系统上的图像的路径。是否可以将图像作为字节数组加载到实体中?例如

@Entity
@Table(name="Users")
public class User
{
   @Id
   @GeneratedValue
   int id;
   String name;
   String pictureName;

   @Transient
   byte[] image;

   // other properties
   public void setPictureName(String pictureName)
   {
        String path="D:\\images\\";
        File f = new File(path + pictureName);
        this.image = new byte[(int)f.length()];

        FileUtility.toByteArray(f,this.image); //custom class

        this.picture = picture;
   }
//other stuff
}

我用 JPA 尝试过,但字节数组图像字段总是为空,而其他一切都很好。

【问题讨论】:

    标签: spring hibernate jpa


    【解决方案1】:

    是的,这是可能的,但是您必须映射具有图片名称的任何列,以便 Hibernate 可以填充它。

    因此,如果您有一个名为“picture_name”的列,那么您的实体应该有:

    @Column(name="picture_name")
    private String pictureName;
    

    然后,当 Hibernate 加载实体时,它会调用setPictureName 方法并运行您的代码将文件加载到字节数组中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-31
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多