【问题标题】:How to update the Lob file in database如何更新数据库中的 Lob 文件
【发布时间】:2022-08-16 17:25:27
【问题描述】:

我已成功在我的数据库中插入一个图像文件,现在我想更改或更新我插入的图像,请帮助我。

这是输出:

Could not set field value [com.mysql.cj.jdbc.Blob@2ab0fa13] 
   value by reflection : [class com.operation.Model.Users.image] setter of com.operation.Model.Users.image; 
nested exception is org.hibernate.PropertyAccessException: Could not set field value [com.mysql.cj.jdbc.Blob@2ab0fa13] 
   value by reflection : [class com.operation.Model.Users.image] setter of com.operation.Model.Users.image

控制器类

@RequestMapping(\"/UpdateData\")
@ResponseBody
public String Updatedata(@RequestParam(\"id\")int id,@RequestParam(\"username\")String username,@RequestParam(\"file\")MultipartFile file) {
    Users myUpdateData = service.getdataByid(id);
    myUpdateData.setUsername(username);
    myUpdateData.setImage(file);
    service.Updatedata(myUpdateData);
    return \"success\";
    
}

服务类

//Update data
public Users Updatedata(Users mod) {
    Users existdata = repo.findByid(mod.getId());
    existdata.setUsername(mod.getUsername());;
    existdata.setImage(mod.getImage());
    return repo.save(existdata);
    
}

存储库

public interface UsersRepo extends JpaRepository<Users, Integer>{

    
    Users findByid(int id);
}

用户类

@Entity
public class Users {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String username;
    
    @Lob
    @Column(columnDefinition = \"MEDIUMBLOB\")
    private String image;
    
    
    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
            
}
  • 你确定图像是字符串吗?通常它是一个字节[]
  • 因此,我将字符串更改为字节,然后将字符串更改为字节后的下一步是什么?
  • 你仍然收到错误吗?

标签: java spring-boot jpa


【解决方案1】:

希望这可以帮助你

实体 :

@Entity
@Data
@NoArgsConstructor

 public class Attachment {

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private long id;

private String file_id;

@Lob
private byte[] data2;
}

存储库:

 @Query("select u from Attachment u where u.file_id = :file_id")
 List<Attachment> findByEmailAddress(@Param("file_id") String file_id);

 @Modifying
@Transactional
@Query("update Attachment u set u.data2 = :data2 where u.file_id = :file_id")
void updatedata2(@Param("file_id") String file_id,@Param("data2") byte[] file);

服务 :

 @Override
public void updateSimilar(String file_id, MultipartFile file) throws IOException {
    attachmentRepository.updatedata2(file_id,file.getBytes());
}

控制器 :

 @PostMapping("/match")
  public ResponseResult matching(@RequestParam("file") MultipartFile file, @RequestParam String file_id ) throws Exception {

  attachmentservice.updateSimilar(file_id, file);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-11
    • 2023-01-22
    • 1970-01-01
    • 2020-11-17
    • 2012-03-31
    • 1970-01-01
    • 2019-11-29
    • 2020-12-05
    相关资源
    最近更新 更多