jpa 如何更新数据库中的Lob文件

iaqfqrcu  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(149)

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

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;
    }
            
}
dxxyhpgq

dxxyhpgq1#

希望这能帮到你
实体:

@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);
}

相关问题